最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 組合控件實現(xiàn)布局的復用的方法

 更新時間:2017年08月01日 16:39:20   作者:echoMu_  
本篇文章主要介紹了Android 組合控件實現(xiàn)布局的復用的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

看到很多項目會有實現(xiàn)自己的標題欄的做法,通常的界面是左邊按鈕或文字,加上中間的標題和右邊的按鈕或文字組成的。比較好的一種做法是使用include標簽,復用同一個xml文件來實現(xiàn)布局的復用。但是這種方法是通過代碼的方式來設置標題,左右按鈕等其他的屬性,會導致布局屬性和Activity代碼耦合性比較高。

因此,我們要通過自定義View,繼承ViewGroup子類來實現(xiàn)這樣的布局,降低布局文件和Activity代碼耦合性。

首先,我們需要寫出布局文件layout_custom_titlebar.xml。

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 使用merge標簽減少層級 -->
<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">
  <!--標題欄背景色-->
  <attr name="title_background_color" format="reference|integer" />
  <!--左邊按鈕是否可見-->
  <attr name="left_button_visible" format="boolean" />
  <!--右邊按鈕是否可見-->
  <attr name="right_button_visible" format="boolean" />
  <!--標題文字-->
  <attr name="title_text" format="string" />
  <!--標題文字顏色-->
  <attr name="title_text_color" format="color" />
  <!--標題文字圖標-->
  <attr name="title_text_drawable" format="reference|integer" />
  <!--左邊按鈕文字-->
  <attr name="left_button_text" format="string" />
  <!--左邊按鈕文字顏色-->
  <attr name="left_button_text_color" format="color" />
  <!--左邊按鈕圖標-->
  <attr name="left_button_drawable" format="reference|integer" />
  <!--右邊按鈕文字-->
  <attr name="right_button_text" format="string" />
  <!--右邊按鈕文字顏色-->
  <attr name="right_button_text_color" format="color" />
  <!--右邊按鈕圖標-->
  <attr name="right_button_drawable" format="reference|integer" />
</declare-styleable>

3.自定義一個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);
    }
    //設置左邊按鈕的文字
    String leftButtonText = typedArray.getString(R.styleable.CustomTitleBar_left_button_text);
    if (!TextUtils.isEmpty(leftButtonText)) {
      titleBarLeftBtn.setText(leftButtonText);
      //設置左邊按鈕文字顏色
      int leftButtonTextColor = typedArray.getColor(R.styleable.CustomTitleBar_left_button_text_color, Color.WHITE);
      titleBarLeftBtn.setTextColor(leftButtonTextColor);
    } else {
      //設置左邊圖片icon 這里是二選一 要么只能是文字 要么只能是圖片
      int leftButtonDrawable = typedArray.getResourceId(R.styleable.CustomTitleBar_left_button_drawable, R.mipmap.titlebar_back_icon);
      if (leftButtonDrawable != -1) {
        titleBarLeftBtn.setBackgroundResource(leftButtonDrawable);
      }
    }

    //先獲取標題是否要顯示圖片icon
    int titleTextDrawable = typedArray.getResourceId(R.styleable.CustomTitleBar_title_text_drawable, -1);
    if (titleTextDrawable != -1) {
      titleBarTitle.setBackgroundResource(titleTextDrawable);
    } else {
      //如果不是圖片標題 則獲取文字標題
      String titleText = typedArray.getString(R.styleable.CustomTitleBar_title_text);
      if (!TextUtils.isEmpty(titleText)) {
        titleBarTitle.setText(titleText);
      }
      //獲取標題顯示顏色
      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);
    }
    //設置右邊按鈕的文字
    String rightButtonText = typedArray.getString(R.styleable.CustomTitleBar_right_button_text);
    if (!TextUtils.isEmpty(rightButtonText)) {
      titleBarRightBtn.setText(rightButtonText);
      //設置右邊按鈕文字顏色
      int rightButtonTextColor = typedArray.getColor(R.styleable.CustomTitleBar_right_button_text_color, Color.BLUE);
      titleBarRightBtn.setTextColor(rightButtonTextColor);
    } else {
      //設置右邊圖片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>


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

相關文章

  • Android模擬器

    Android模擬器"Failed To Allocate memory 8"錯誤如何解決

    這篇文章主要介紹了Android模擬器"Failed To Allocate memory 8"錯誤如何解決的相關資料,需要的朋友可以參考下
    2017-03-03
  • Android自定義Toast樣式實現(xiàn)方法詳解

    Android自定義Toast樣式實現(xiàn)方法詳解

    這篇文章主要介紹了Android自定義Toast樣式,Toast是一種很方便的消息提示框,會在 屏幕中顯示一個消息提示框,沒任何按鈕,也不會獲得焦點一段時間過后自動消失!非常常用!本文就來通過一個例子把Toast的使用講透
    2023-01-01
  • Android應用開發(fā)中Fragment存儲功能的基本用法

    Android應用開發(fā)中Fragment存儲功能的基本用法

    這篇文章主要介紹了Android應用開發(fā)中使用Fragment存儲功能的基本用法,包括對Fragment的非中斷保存setRetaineInstance的講解,需要的朋友可以參考下
    2016-02-02
  • flutter 自定義websocket路由的實現(xiàn)

    flutter 自定義websocket路由的實現(xiàn)

    這篇文章主要介紹了flutter 自定義websocket路由的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-12-12
  • Android GridView實現(xiàn)橫向列表水平滾動

    Android GridView實現(xiàn)橫向列表水平滾動

    這篇文章主要為大家詳細介紹了Android GridView實現(xiàn)橫向列表水平滾動,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android?Banner本地和網絡輪播圖使用介紹

    Android?Banner本地和網絡輪播圖使用介紹

    大家好,本篇文章講的是Android?Banner本地和網絡輪播圖使用介紹,感興趣的同學趕快來看一看吧,希望本篇文章對你起到幫助
    2021-11-11
  • Android實現(xiàn)兩個數(shù)相加功能

    Android實現(xiàn)兩個數(shù)相加功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)兩個數(shù)相加功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • Android recyclerView橫條指示器實現(xiàn)淘寶菜單模塊

    Android recyclerView橫條指示器實現(xiàn)淘寶菜單模塊

    這篇文章主要為大家詳細介紹了recyclerView橫條指示器實現(xiàn)淘寶菜單模塊,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • WebView渲染異常導致閃退問題的解決方案

    WebView渲染異常導致閃退問題的解決方案

    這篇文章主要介紹了WebView渲染異常導致閃退問題的解決方案,文中通過設置WebViewClient重寫onRenderProcessGone()方法并移除、重新創(chuàng)建Web容器來解決該問題,需要的朋友可以參考下
    2025-02-02
  • Android仿微信支付密碼彈出層功能

    Android仿微信支付密碼彈出層功能

    最近項目中使用到了支付密碼功能,感覺這類界面應該是比較常用的,涉及支付密碼的輸入的一般都會用到吧,所以單獨地把這部分抽取出來,有需要的朋友可以拿去用用
    2017-04-04

最新評論

运城市| 宁安市| 灵寿县| 淮滨县| 桦南县| 东莞市| 婺源县| 新绛县| 乌鲁木齐市| 霍邱县| 会昌县| 宜州市| 宣城市| 石城县| 云梦县| 怀化市| 乐业县| 广灵县| 乐东| 图木舒克市| 齐齐哈尔市| 邵阳市| 阿瓦提县| 米易县| 辉县市| 新安县| 和田县| 台山市| 邯郸市| 乌拉特中旗| 囊谦县| 滕州市| 宝坻区| 垣曲县| 香河县| 双峰县| 阳春市| 石泉县| 渭南市| 交城县| 西平县|