Android自定義通用標題欄CustomTitleBar
更新時間:2018年11月20日 16:39:03 作者:Li的小寶寶
這篇文章主要為大家詳細介紹了Android自定義通用標題欄CustomTitleBar,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android自定義通用標題欄的具體代碼,供大家參考,具體內容如下/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)一頁面標題欄,項目中包括接口回調
public class CustomerTitleBar extends LinearLayout { private LinearLayout rootView; private ImageView ivLeft; private TextView tvTitle; private TextView tvRight; //3.聲明回調對象 private CustomerClick leftClick; /** * @param context */ //在代碼中直接new一個Custom View實例的時候,會調用第一個構造函數(shù) public CustomerTitleBar(Context context) { this(context,null); } //在xml布局文件中調用Custom View的時候,會調用第二個構造函數(shù) public CustomerTitleBar(Context context,AttributeSet attrs) { this(context, attrs,-1); } //在xml布局文件中調用Custom View,并且Custom View標簽中還有自定義屬性時,這里調用的還是第二個構造函數(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.使用接口回調 if (leftClick!=null){ leftClick.onLefClick(v); } } }); } //4、提供回調對象的set方法 public void setLeftClick(CustomerClick leftClick) { this.leftClick = leftClick; } //1.定義回調接口 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(); } }); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android自定義實現(xiàn)循環(huán)滾輪控件WheelView
滾輪布局WheelView大家經常使用,比如在選擇生日的時候,風格類似系統(tǒng)提供的DatePickerDialog,這篇文章主要為大家詳細介紹了Android自定義實現(xiàn)循環(huán)滾輪控件WheelView,感興趣的小伙伴們可以參考一下2016-07-07Android編程添加快捷方式(Short)到手機桌面的方法(含添加,刪除及查詢)
這篇文章主要介紹了Android編程添加快捷方式(Short)到手機桌面的方法,含有針對桌面快捷方式的添加,刪除及查詢的操作實現(xiàn)技巧,需要的朋友可以參考下2016-01-01