Android自定義通用標(biāo)題欄CustomTitleBar
本文實(shí)例為大家分享了Android自定義通用標(biāo)題欄的具體代碼,供大家參考,具體內(nèi)容如下/p>
1自定義一個public_titlebar.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rootView" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/ivLeft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/z"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <TextView android:id="@+id/tvTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="tvTitle"/> <TextView android:id="@+id/tvRight" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="tvRight"/> </LinearLayout> </LinearLayout>
2.在values文件夾下新建一個attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomerTitleBar"> <attr name="left_image" format="reference"></attr> <attr name="center_text" format="string"></attr> <attr name="center_text_color" format="color"></attr> <attr name="center_text_size" format="dimension"></attr> </declare-styleable> </resources>
3.自定義CustomerTitleBar類繼承LinearLayout,統(tǒng)一頁面標(biāo)題欄,項(xiàng)目中包括接口回調(diào)
public class CustomerTitleBar extends LinearLayout { private LinearLayout rootView; private ImageView ivLeft; private TextView tvTitle; private TextView tvRight; //3.聲明回調(diào)對象 private CustomerClick leftClick; /** * @param context */ //在代碼中直接new一個Custom View實(shí)例的時候,會調(diào)用第一個構(gòu)造函數(shù) public CustomerTitleBar(Context context) { this(context,null); } //在xml布局文件中調(diào)用Custom View的時候,會調(diào)用第二個構(gòu)造函數(shù) public CustomerTitleBar(Context context,AttributeSet attrs) { this(context, attrs,-1); } //在xml布局文件中調(diào)用Custom View,并且Custom View標(biāo)簽中還有自定義屬性時,這里調(diào)用的還是第二個構(gòu)造函數(shù). public CustomerTitleBar(Context context,AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); initAttrs(context,attrs); initLister(); } private void init(Context context) { rootView= (LinearLayout) View.inflate(context,R.layout.layout_customer_title_bar,this); ivLeft=rootView.findViewById(R.id.ivLeft); tvTitle=rootView.findViewById(R.id.tvTitle); tvRight=rootView.findViewById(R.id.tvRight); } private void initAttrs(Context context, AttributeSet attrs) { TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.CustomerTitleBar); Drawable drawable=typedArray.getDrawable(R.styleable.CustomerTitleBar_left_image); if (drawable!=null){ ivLeft.setImageDrawable(drawable); } CharSequence text = typedArray.getText(R.styleable.CustomerTitleBar_center_text); if (!TextUtils.isEmpty(text)) { tvTitle.setText(text); } int color = typedArray.getColor(R.styleable.CustomerTitleBar_center_text_color, -1); if (color != -1) { tvTitle.setTextColor(color); } float dimension = typedArray.getDimension(R.styleable.CustomerTitleBar_center_text_size, 0f); tvTitle.setTextSize(dimension); } private void initLister() { ivLeft.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //5.使用接口回調(diào) if (leftClick!=null){ leftClick.onLefClick(v); } } }); } //4、提供回調(diào)對象的set方法 public void setLeftClick(CustomerClick leftClick) { this.leftClick = leftClick; } //1.定義回調(diào)接口 interface CustomerClick{ void onLefClick(View view); } }
4.在布局文件中的引用
<com.cn.jyx.customertitlebar.CustomerTitleBar android:id="@+id/ctTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" customer:center_text="吐泡泡" customer:center_text_color="#ff00ff" customer:center_text_size="20sp" />
5.在Activity中的用法
public class MainActivity extends AppCompatActivity { private CustomerTitleBar ctTitle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ctTitle=findViewById(R.id.ctTitle); //6、使用 ctTitle.setLeftClick(new CustomerTitleBar.CustomerClick() { @Override public void onLefClick(View view) { Toast.makeText(MainActivity.this,"吐泡泡",Toast.LENGTH_LONG).show(); } }); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義復(fù)合控件實(shí)現(xiàn)通用標(biāo)題欄
- Android組合控件自定義標(biāo)題欄
- Android中自定義標(biāo)題欄樣式的兩種方法
- 3種Android隱藏頂部狀態(tài)欄及標(biāo)題欄的方法
- Android自定義狀態(tài)欄顏色與應(yīng)用標(biāo)題欄顏色一致
- Android中去掉標(biāo)題欄的幾種方法(三種)
- Android中隱藏標(biāo)題欄和狀態(tài)欄的方法
- Android 全屏無標(biāo)題欄的三種實(shí)現(xiàn)方法
- Android 頂部標(biāo)題欄隨滑動時的漸變隱藏和漸變顯示效果
- Android自定義簡單的頂部標(biāo)題欄
相關(guān)文章
Android實(shí)現(xiàn)隱藏手機(jī)底部虛擬按鍵
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)隱藏手機(jī)底部虛擬按鍵,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08Android自定義實(shí)現(xiàn)循環(huán)滾輪控件WheelView
滾輪布局WheelView大家經(jīng)常使用,比如在選擇生日的時候,風(fēng)格類似系統(tǒng)提供的DatePickerDialog,這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)循環(huán)滾輪控件WheelView,感興趣的小伙伴們可以參考一下2016-07-07Android獲取分享應(yīng)用列表詳解及實(shí)例
這篇文章主要介紹了Android獲取分享應(yīng)用列表詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04Android提高之藍(lán)牙傳感應(yīng)用實(shí)例
這篇文章主要介紹了Android的藍(lán)牙傳感應(yīng)用實(shí)例,對于Android項(xiàng)目開發(fā)來說非常實(shí)用,需要的朋友可以參考下2014-08-08Android編程添加快捷方式(Short)到手機(jī)桌面的方法(含添加,刪除及查詢)
這篇文章主要介紹了Android編程添加快捷方式(Short)到手機(jī)桌面的方法,含有針對桌面快捷方式的添加,刪除及查詢的操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01Android實(shí)踐之帶加載效果的下拉刷新上拉加載更多
這篇文章主要給大家介紹了關(guān)于Android實(shí)踐之下拉刷新上拉加載更多的相關(guān)資料,實(shí)現(xiàn)的效果在現(xiàn)在的很多項(xiàng)目中都能用到,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12Android實(shí)現(xiàn)左滑關(guān)閉窗口
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)左滑關(guān)閉窗口,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04Android調(diào)用系統(tǒng)照相機(jī)拍照與攝像的方法
這篇文章主要為大家詳細(xì)介紹了Android如何調(diào)用系統(tǒng)現(xiàn)有的照相機(jī)拍照與攝像,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04