色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Android Studio 創建自定義控件的方法

瀏覽:2日期:2022-09-23 16:08:49

我們知道,當系統控件并不能滿足我們的需求時,我們就需要來創建自定義控件,主要有兩種方法

(1)引入布局

下面來自定義一個控件,iPhone的標題欄,創建一個標題欄并不是什么難事,加入兩個button一個TextView就行了,可是在我們的應用中,有很多頁面都是需要這樣的標題欄,我們不可能每個活動都寫一遍布局,這個時候我們就可以用引用布局的方法,新建一個title.xml

<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' android:layout_width='match_parent' android:layout_height='wrap_content' android:background='#817D7D' > <Button android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_gravity='center' android:layout_margin='5dp' android:text='back' android:textColor='#fff'/> <TextView android: android:layout_width='0dp' android:layout_height='wrap_content' android:layout_gravity='center' android:layout_weight='1' android:gravity='center' android:textColor='#c0c0c0' android:textSize='24sp' android:text='title text' /> <Button android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_gravity='center' android:layout_margin='5dp' android:textColor='#fff' android:text='edit' /></LinearLayout>

現在標題欄已經寫好了,接下來就要在程序中使用,修改activity_main.xml

<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' android:layout_width='match_parent' android:layout_height='match_parent' > <include layout='@layout/title'/></LinearLayout>

我們只要通過一句include語句引進來就行了

<include layout='@layout/title'/>

最后我們需要在MainActivity中將系統自帶的標題欄屏蔽

package com.example.ch03;import android.drm.DrmStore;import android.support.v7.app.ActionBar;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //屏蔽系統自帶狀態欄 ActionBar actionBar = getSupportActionBar(); if(actionBar != null){ actionBar.hide(); } }}

最后來看一下效果

Android Studio 創建自定義控件的方法

(2)注冊點擊事件

在上面我們看到,每個界面的返回按鈕功能都是一樣的,即銷毀當前活動,我們不可能在每個活動中都重新注冊,所以使用自定義控件的方式來解決新建TitleLayout,成為標題欄控件

public class TitleLayout extends LinearLayout {public TitleLayout(Context context, AttributeSet attrs){ super(context,attrs); LayoutInflater.from(context).inflate(R.layout.title,this);

我們重寫了LinearLayout中帶參數的構造函數,引入TitleLayout控件就會調用這個構造函數,然后對標題欄進行動態加載,就需要借助LayoutInflater實現。通過LayoutInflater的from方法構建一個LayoutInflater對象,調用inflate()方法動態加載一個布局文件

然后在布局文件中添加自定義控件,修改activity_main.xml

<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' android:layout_width='match_parent' android:layout_height='match_parent' ><com.example.ch03.TitleLayout android:layout_width='match_parent' android:layout_height='wrap_content'/></LinearLayout>

重新運行一下,效果是一樣的

下面來給按鈕注冊點擊事件,修改TitleLayout中的代碼

package com.example.ch03;import android.app.Activity;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;import android.widget.Toast;public class TitleLayout extends LinearLayout {public TitleLayout(Context context, AttributeSet attrs){ super(context,attrs); LayoutInflater.from(context).inflate(R.layout.title,this); Button titleBack = findViewById(R.id.title_back); Button titleEdit = findViewById(R.id.title_edit); titleBack.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ((Activity) getContext()).finish(); } }); titleEdit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(),'You click edit button', Toast.LENGTH_LONG).show(); } });}}

重新運行一下,然后點擊edit按鈕

Android Studio 創建自定義控件的方法

到此這篇關于Android Studio 創建自定義控件的方法的文章就介紹到這了,更多相關Android Studio自定義控件內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
主站蜘蛛池模板: 伊人久久91| 99九九精品国产高清自在线 | 亚洲综合一 | 99久久精品免费看国产一区二区 | 欧美午夜毛片a级在线 | 久久免费观看国产精品 | 另类一区二区三区 | 久久综合本色宗合一本色 | 成人综合国产乱在线 | 国产a级特黄的片子视频免费 | 日韩精品一区二区三区中文在线 | 亚洲成人在线免费视频 | 亚洲一区二区三区视频 | 黄色片亚洲 | 手机看片毛片 | 国产露脸3p普通话 | 香港aa三级久久三级老师 | 久久思思爱 | 欧美aaaaa一级毛片在线 | 国产三及 | 一级高清毛片免费a级高清毛片 | 国内成人精品亚洲日本语音 | 日韩一级性生活片 | 久久99精品久久久久久青青91 | 欧美最爽乱淫视频播放黑人 | 成人免费久久精品国产片久久影院 | 久久久久成人精品一区二区 | 九九九九在线视频播放 | 男女视频免费网站 | 精品一区二区三区在线视频观看 | 最新国产精品好看的国产精品 | 欧美激情特级黄aa毛片 | 国内精品视频成人一区二区 | 香蕉久久久久久狠狠色 | 女高中生被cao到哭视频 | 国产成人久久精品二区三区牛 | 免费看欧美一级特黄a毛片 免费看片aⅴ免费大片 | 成人黄色三级 | a毛片免费观看 | 日本一级特黄大一片免 | 成人午夜精品 |