国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義通用標題欄CustomTitleBar

2019-10-21 21:35:45
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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,統一頁面標題欄,項目中包括接口回調

 

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實例的時候,會調用第一個構造函數public CustomerTitleBar(Context context) { this(context,null);}//在xml布局文件中調用Custom View的時候,會調用第二個構造函數public CustomerTitleBar(Context context,AttributeSet attrs) { this(context, attrs,-1);}//在xml布局文件中調用Custom View,并且Custom View標簽中還有自定義屬性時,這里調用的還是第二個構造函數.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();   }  });}}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄冈市| 高尔夫| 黄大仙区| 隆子县| 思茅市| 仙桃市| 叶城县| 黄浦区| 咸丰县| 九龙县| 贵定县| 浪卡子县| 永泰县| 穆棱市| 苏尼特左旗| 清远市| 白朗县| 泰安市| 台安县| 饶阳县| 永德县| 五寨县| 郓城县| 浪卡子县| 武强县| 丹东市| 界首市| 通城县| 新蔡县| 玉林市| 会宁县| 潍坊市| 黄梅县| 霍山县| 山西省| 长沙县| 论坛| 河源市| 梅河口市| 六枝特区| 平山县|