Android自定義控件的創(chuàng)建方法
本文為大家分享了Android創(chuàng)建自定義控件的具體代碼,供大家參考,具體內(nèi)容如下
1、仿iPhone 的風(fēng)格,在界面的頂部放置一個(gè)標(biāo)題欄。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#2197db" android:orientation="horizontal" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"> <Button android:id="@+id/title_back" android:layout_width="90dp" android:layout_height="40dp" android:layout_gravity="center" android:layout_margin="5dp" android:text="返回" /> <TextView android:id="@+id/title_text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="標(biāo)題" android:textColor="#fff" android:textSize="24sp" /> <Button android:id="@+id/title_edit" android:layout_width="90dp" android:layout_height="40dp" android:layout_gravity="center" android:layout_margin="5dp" android:text="確定" /> </LinearLayout> </RelativeLayout>
標(biāo)題欄布局已經(jīng)編寫完成,剩下的就是如何在程序中使用這個(gè)標(biāo)題欄。
<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語句將標(biāo)題欄布局引入進(jìn)來就可以了。
然后在 MainActivity 中將系統(tǒng)自帶的標(biāo)題欄隱藏掉
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); } }
我們還是需要在每個(gè)活動(dòng)中為這些控件單獨(dú)編寫一次事件注冊的代碼。比如說標(biāo)題欄中的返回按鈕,其實(shí)不管是在哪一個(gè)活動(dòng)中,這個(gè)按鈕的功能都是相同的,即銷毀掉當(dāng)前活動(dòng),這種情況最好是使用自定義控件的方式來解決。
新建自定義的標(biāo)題欄控件:
public class TitleLayout extends LinearLayout { public TitleLayout(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.title, this); } }
我們重寫了 LinearLayout 中的帶有兩個(gè)參數(shù)的構(gòu)造函數(shù),在布局中引入 TitleLayout控件就會(huì)調(diào)用這個(gè)構(gòu)造函數(shù)。然后在構(gòu)造函數(shù)中需要對標(biāo)題欄布局進(jìn)行動(dòng)態(tài)加載,這就要借助 LayoutInflater 來實(shí)現(xiàn)了。通過 LayoutInflater 的 from()方法可以構(gòu)建出一個(gè) LayoutInflater對象,然后調(diào)用 inflate()方法就可以動(dòng)態(tài)加載一個(gè)布局文件,inflate()方法接收兩個(gè)參數(shù),第一個(gè)參數(shù)是要加載的布局文件的 id,這里我們傳入 R.layout.title,第二個(gè)參數(shù)是給加載好的布局再添加一個(gè)父布局,這里我們想要指定為 TitleLayout,于是直接傳入this
在布局文件中添加這個(gè)自定義控件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.xxxxxx.TitleLayout android:layout_width="match_parent" android:layout_height="wrap_content" ></com.example.xxxxxx.TitleLayout> </LinearLayout>
我們來嘗試為標(biāo)題欄中的按鈕注冊點(diǎn)擊事件,修改 TitleLayout中的代碼
public class TitleLayout extends LinearLayout { public TitleLayout(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.title, this); Button titleBack = (Button) findViewById(R.id.title_back); Button titleEdit = (Button) findViewById(R.id.title_edit); titleBack.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ((Activity) getContext()).finish(); } }); titleEdit.setOnClickListener(new OnClickListener() { public static final String TAG = ""; @Override public void onClick(View v) { Toast.makeText(getContext(), "重新運(yùn)行程序", Toast.LENGTH_SHORT).show(); Log.i(TAG, "111 "); } }); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義控件之創(chuàng)建可復(fù)用的組合控件
- Android自定義控件之繼承ViewGroup創(chuàng)建新容器
- android自定義控件創(chuàng)建翻頁接口詳細(xì)代碼
- android 自定義控件 自定義屬性詳細(xì)介紹
- android開發(fā)教程之自定義控件checkbox的樣式示例
- android自定義控件和自定義回調(diào)函數(shù)步驟示例
- Android中自定義控件的declare-styleable屬性重用方案
- 詳解Android自定義控件屬性TypedArray以及attrs
- Android開發(fā)之自定義控件用法詳解
- Android自定義控件之圓形/圓角的實(shí)現(xiàn)代碼
相關(guān)文章
Android開發(fā)Dart?Constructors構(gòu)造函數(shù)使用技巧整理
這篇文章主要為大家介紹了Android開發(fā)Dart?Constructors構(gòu)造函數(shù)使用技巧整理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Android實(shí)現(xiàn)調(diào)取支付寶健康碼
大家好,本篇文章主要講的是Android實(shí)現(xiàn)調(diào)取支付寶健康碼,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01Android應(yīng)用中clearFocus方法調(diào)用無效的問題解決
clearFocus()主要用于清除EditText的焦點(diǎn),Android App開發(fā)中很多時(shí)候會(huì)發(fā)現(xiàn)其調(diào)用無效,帶著這個(gè)問題我們就來看一下本文主題、Android應(yīng)用中clearFocus方法調(diào)用無效的問題解決2016-05-05Android實(shí)現(xiàn)彈出列表、單選、多選框
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)彈出列表、單選、多選框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10Android編程實(shí)現(xiàn)EditText字?jǐn)?shù)監(jiān)聽并顯示的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)EditText字?jǐn)?shù)監(jiān)聽并顯示的方法,涉及Android EditText文本框事件監(jiān)聽與響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2017-02-02