Android 組合控件實(shí)現(xiàn)布局的復(fù)用的方法
看到很多項(xiàng)目會(huì)有實(shí)現(xiàn)自己的標(biāo)題欄的做法,通常的界面是左邊按鈕或文字,加上中間的標(biāo)題和右邊的按鈕或文字組成的。比較好的一種做法是使用include標(biāo)簽,復(fù)用同一個(gè)xml文件來(lái)實(shí)現(xiàn)布局的復(fù)用。但是這種方法是通過(guò)代碼的方式來(lái)設(shè)置標(biāo)題,左右按鈕等其他的屬性,會(huì)導(dǎo)致布局屬性和Activity代碼耦合性比較高。
因此,我們要通過(guò)自定義View,繼承ViewGroup子類來(lái)實(shí)現(xiàn)這樣的布局,降低布局文件和Activity代碼耦合性。
首先,我們需要寫(xiě)出布局文件layout_custom_titlebar.xml。
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 使用merge標(biāo)簽減少層級(jí) --> <Button android:id="@+id/title_bar_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:background="@null" android:minHeight="45dp" android:minWidth="45dp" android:textSize="14sp" /> <TextView android:id="@+id/title_bar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:singleLine="true" android:textSize="17sp" /> <Button android:id="@+id/title_bar_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="7dp" android:background="@null" android:minHeight="45dp" android:minWidth="45dp" android:textSize="14sp" /> </merge>
2.定義自定義屬性
<declare-styleable name="CustomTitleBar"> <!--標(biāo)題欄背景色--> <attr name="title_background_color" format="reference|integer" /> <!--左邊按鈕是否可見(jiàn)--> <attr name="left_button_visible" format="boolean" /> <!--右邊按鈕是否可見(jiàn)--> <attr name="right_button_visible" format="boolean" /> <!--標(biāo)題文字--> <attr name="title_text" format="string" /> <!--標(biāo)題文字顏色--> <attr name="title_text_color" format="color" /> <!--標(biāo)題文字圖標(biāo)--> <attr name="title_text_drawable" format="reference|integer" /> <!--左邊按鈕文字--> <attr name="left_button_text" format="string" /> <!--左邊按鈕文字顏色--> <attr name="left_button_text_color" format="color" /> <!--左邊按鈕圖標(biāo)--> <attr name="left_button_drawable" format="reference|integer" /> <!--右邊按鈕文字--> <attr name="right_button_text" format="string" /> <!--右邊按鈕文字顏色--> <attr name="right_button_text_color" format="color" /> <!--右邊按鈕圖標(biāo)--> <attr name="right_button_drawable" format="reference|integer" /> </declare-styleable>
3.自定義一個(gè)View繼承ViewGroup子類,這里我們繼承RelativeLayout。
public class CustomTitleBar extends RelativeLayout { private Button titleBarLeftBtn; private Button titleBarRightBtn; private TextView titleBarTitle; public CustomTitleBar(Context context) { super(context); } public CustomTitleBar(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.layout_custom_titlebar,this,true); titleBarLeftBtn = (Button) findViewById(R.id.title_bar_left); titleBarRightBtn = (Button) findViewById(R.id.title_bar_right); titleBarTitle = (TextView) findViewById(R.id.title_bar_title); TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.CustomTitleBar); if(typedArray!=null){ //titleBar背景色 int titleBarBackGround=typedArray.getResourceId(R.styleable.CustomTitleBar_title_background_color, Color.BLUE); setBackgroundColor(titleBarBackGround); //獲取是否要顯示左邊按鈕 boolean leftButtonVisible = typedArray.getBoolean(R.styleable.CustomTitleBar_left_button_visible, true); if (leftButtonVisible) { titleBarLeftBtn.setVisibility(View.VISIBLE); } else { titleBarLeftBtn.setVisibility(View.INVISIBLE); } //設(shè)置左邊按鈕的文字 String leftButtonText = typedArray.getString(R.styleable.CustomTitleBar_left_button_text); if (!TextUtils.isEmpty(leftButtonText)) { titleBarLeftBtn.setText(leftButtonText); //設(shè)置左邊按鈕文字顏色 int leftButtonTextColor = typedArray.getColor(R.styleable.CustomTitleBar_left_button_text_color, Color.WHITE); titleBarLeftBtn.setTextColor(leftButtonTextColor); } else { //設(shè)置左邊圖片icon 這里是二選一 要么只能是文字 要么只能是圖片 int leftButtonDrawable = typedArray.getResourceId(R.styleable.CustomTitleBar_left_button_drawable, R.mipmap.titlebar_back_icon); if (leftButtonDrawable != -1) { titleBarLeftBtn.setBackgroundResource(leftButtonDrawable); } } //先獲取標(biāo)題是否要顯示圖片icon int titleTextDrawable = typedArray.getResourceId(R.styleable.CustomTitleBar_title_text_drawable, -1); if (titleTextDrawable != -1) { titleBarTitle.setBackgroundResource(titleTextDrawable); } else { //如果不是圖片標(biāo)題 則獲取文字標(biāo)題 String titleText = typedArray.getString(R.styleable.CustomTitleBar_title_text); if (!TextUtils.isEmpty(titleText)) { titleBarTitle.setText(titleText); } //獲取標(biāo)題顯示顏色 int titleTextColor = typedArray.getColor(R.styleable.CustomTitleBar_title_text_color, Color.WHITE); titleBarTitle.setTextColor(titleTextColor); } //獲取是否要顯示右邊按鈕 boolean rightButtonVisible = typedArray.getBoolean(R.styleable.CustomTitleBar_right_button_visible, true); if (rightButtonVisible) { titleBarRightBtn.setVisibility(View.VISIBLE); } else { titleBarRightBtn.setVisibility(View.INVISIBLE); } //設(shè)置右邊按鈕的文字 String rightButtonText = typedArray.getString(R.styleable.CustomTitleBar_right_button_text); if (!TextUtils.isEmpty(rightButtonText)) { titleBarRightBtn.setText(rightButtonText); //設(shè)置右邊按鈕文字顏色 int rightButtonTextColor = typedArray.getColor(R.styleable.CustomTitleBar_right_button_text_color, Color.BLUE); titleBarRightBtn.setTextColor(rightButtonTextColor); } else { //設(shè)置右邊圖片icon 這里是二選一 要么只能是文字 要么只能是圖片 int rightButtonDrawable = typedArray.getResourceId(R.styleable.CustomTitleBar_right_button_drawable, -1); if (rightButtonDrawable != -1) { titleBarRightBtn.setBackgroundResource(rightButtonDrawable); } } typedArray.recycle(); } } public void setTitleClickListener(OnClickListener onClickListener) { if (onClickListener != null) { titleBarLeftBtn.setOnClickListener(onClickListener); titleBarRightBtn.setOnClickListener(onClickListener); } } public Button getTitleBarLeftBtn() { return titleBarLeftBtn; } public Button getTitleBarRightBtn() { return titleBarRightBtn; } public TextView getTitleBarTitle() { return titleBarTitle; } }
4.正確地使用它
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.mumubin.demoproject.view.CustomTitleBar android:id="@+id/ctb_view" android:layout_width="match_parent" android:layout_height="45dp" app:right_button_drawable="@mipmap/sure" app:title_text="@string/app_name" /> <com.mumubin.demoproject.view.CustomTitleBar android:layout_width="match_parent" android:layout_height="45dp" android:layout_marginTop="4dp" app:title_background_color="@color/colorPrimary" app:title_text="@string/app_name" app:title_text_color="@color/colorAccent" app:left_button_text="左邊" app:right_button_text="右邊"/> <com.mumubin.demoproject.view.CustomTitleBar android:layout_width="match_parent" android:layout_height="45dp" android:layout_marginTop="4dp" app:title_text_drawable="@mipmap/ic_launcher" app:title_background_color="@color/colorAccent" app:left_button_text="左邊" app:right_button_text="右邊"/> </LinearLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android動(dòng)態(tài)添加設(shè)置布局與控件的方法
- Android開(kāi)發(fā)之基本控件和四種布局方式詳解
- Android 布局控件之LinearLayout詳細(xì)介紹
- Android 圖片網(wǎng)格布局控件示例代碼
- Android布局控件之常用linearlayout布局
- Android布局優(yōu)化之ViewStub控件
- Android編程布局控件之AbsoluteLayout用法實(shí)例分析
- Android 仿京東商城底部布局的選擇效果(Selector 選擇器的實(shí)現(xiàn))
- Android時(shí)間選擇器、日期選擇器實(shí)現(xiàn)代碼
- Android開(kāi)發(fā)實(shí)現(xiàn)布局中為控件添加選擇器的方法
相關(guān)文章
Android Activity進(jìn)出動(dòng)畫(huà)三種方法
這篇文章主要介紹了Android Activity進(jìn)出動(dòng)畫(huà)三種方法的相關(guān)資料,需要的朋友可以參考下2017-05-05Android安裝apk文件并適配Android 7.0詳解
這篇文章主要介紹了Android安裝apk文件并適配Android 7.0詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05Android開(kāi)發(fā)系列二之窗口Activity的生命周期
這篇文章主要介紹了Android學(xué)習(xí)系列二之窗口Activity的生命周期的相關(guān)資料,需要的朋友可以參考下2016-05-05Android UI手機(jī)信息頁(yè)面設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了Android UI手機(jī)信息頁(yè)面的設(shè)計(jì)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03android實(shí)現(xiàn)節(jié)點(diǎn)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)節(jié)點(diǎn)進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05UIImage初始化的區(qū)別兩種方法介紹(面試常見(jiàn))
本文通過(guò)兩種方法給大家介紹UIImage初始化的區(qū)別,在面試過(guò)程中經(jīng)常遇到,對(duì)uiimage初始化相關(guān)知識(shí)感興趣的朋友一起了解下吧2016-05-05