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)編寫(xiě)完成,剩下的就是如何在程序中使用這個(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> //我們只需要通過(guò)一行 include語(yǔ)句將標(biāo)題欄布局引入進(jìn)來(lái)就可以了。
然后在 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ú)編寫(xiě)一次事件注冊(cè)的代碼。比如說(shuō)標(biāo)題欄中的返回按鈕,其實(shí)不管是在哪一個(gè)活動(dòng)中,這個(gè)按鈕的功能都是相同的,即銷毀掉當(dāng)前活動(dòng),這種情況最好是使用自定義控件的方式來(lái)解決。
新建自定義的標(biāo)題欄控件:
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);
}
}
我們重寫(xiě)了 LinearLayout 中的帶有兩個(gè)參數(shù)的構(gòu)造函數(shù),在布局中引入 TitleLayout控件就會(huì)調(diào)用這個(gè)構(gòu)造函數(shù)。然后在構(gòu)造函數(shù)中需要對(duì)標(biāo)題欄布局進(jìn)行動(dòng)態(tài)加載,這就要借助 LayoutInflater 來(lái)實(shí)現(xiàn)了。通過(guò) LayoutInflater 的 from()方法可以構(gòu)建出一個(gè) LayoutInflater對(duì)象,然后調(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>
我們來(lái)嘗試為標(biāo)題欄中的按鈕注冊(cè)點(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 ");
}
});
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義控件之創(chuàng)建可復(fù)用的組合控件
- Android自定義控件之繼承ViewGroup創(chuàng)建新容器
- android自定義控件創(chuàng)建翻頁(yè)接口詳細(xì)代碼
- android 自定義控件 自定義屬性詳細(xì)介紹
- android開(kāi)發(fā)教程之自定義控件checkbox的樣式示例
- android自定義控件和自定義回調(diào)函數(shù)步驟示例
- Android中自定義控件的declare-styleable屬性重用方案
- 詳解Android自定義控件屬性TypedArray以及attrs
- Android開(kāi)發(fā)之自定義控件用法詳解
- Android自定義控件之圓形/圓角的實(shí)現(xiàn)代碼
相關(guān)文章
Android倒計(jì)時(shí)功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android倒計(jì)時(shí)功能的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
Android實(shí)現(xiàn)點(diǎn)贊動(dòng)畫(huà)(27)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)點(diǎn)贊動(dòng)畫(huà),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android SharedPreferences存儲(chǔ)用法詳解
這篇文章主要為大家詳細(xì)介紹了Android SharedPreferences存儲(chǔ)用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android?Flutter實(shí)現(xiàn)頁(yè)面切換轉(zhuǎn)場(chǎng)動(dòng)畫(huà)效果
Hero組件非常適合從列表、概覽頁(yè)切換到詳情頁(yè)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)場(chǎng)合。本文將利用Hero組件制作一個(gè)簡(jiǎn)單的頁(yè)面切換轉(zhuǎn)場(chǎng)動(dòng)畫(huà)效果,感興趣的可以了解一下2022-06-06
Android中SharedPreference使用實(shí)例講解
這篇文章主要介紹了Android中SharedPreference使用方法,實(shí)現(xiàn)登陸界面記住密碼功能,感興趣的小伙伴們可以參考一下2016-01-01
Flutter利用Canvas模擬實(shí)現(xiàn)微信紅包領(lǐng)取效果
這篇文章主要為大家詳細(xì)介紹了如何利用Flutter中的Canvas模擬實(shí)現(xiàn)微信紅包領(lǐng)取的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-03-03
Android自定義View實(shí)現(xiàn)仿GitHub的提交活躍表格
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)仿GitHub的提交活躍表格,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-01-01
android 把float轉(zhuǎn)換成Int的實(shí)例講解
今天小編就為大家分享一篇android 把float轉(zhuǎn)換成Int的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07

