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

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

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

看到很多項(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android模擬器

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

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

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

    這篇文章主要介紹了Android自定義Toast樣式,Toast是一種很方便的消息提示框,會(huì)在 屏幕中顯示一個(gè)消息提示框,沒(méi)任何按鈕,也不會(huì)獲得焦點(diǎn)一段時(shí)間過(guò)后自動(dòng)消失!非常常用!本文就來(lái)通過(guò)一個(gè)例子把Toast的使用講透
    2023-01-01
  • Android應(yīng)用開(kāi)發(fā)中Fragment存儲(chǔ)功能的基本用法

    Android應(yīng)用開(kāi)發(fā)中Fragment存儲(chǔ)功能的基本用法

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

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

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

    Android GridView實(shí)現(xiàn)橫向列表水平滾動(dòng)

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

    Android?Banner本地和網(wǎng)絡(luò)輪播圖使用介紹

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

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

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

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

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

    WebView渲染異常導(dǎo)致閃退問(wèn)題的解決方案

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

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

    最近項(xiàng)目中使用到了支付密碼功能,感覺(jué)這類界面應(yīng)該是比較常用的,涉及支付密碼的輸入的一般都會(huì)用到吧,所以單獨(dú)地把這部分抽取出來(lái),有需要的朋友可以拿去用用
    2017-04-04

最新評(píng)論

普兰店市| 五华县| 南宁市| 桐庐县| 望谟县| 马鞍山市| 乌苏市| 安阳市| 锦州市| 阳山县| 丹巴县| 台中市| 拜泉县| 乐清市| 泰兴市| 齐齐哈尔市| 宁陵县| 山阳县| 镇坪县| 航空| 赤城县| 石家庄市| 宽甸| 涟水县| 五河县| 紫云| 观塘区| 商水县| 饶河县| 道孚县| 鄂伦春自治旗| 定远县| 手机| 永福县| 游戏| 侯马市| 白朗县| 翼城县| 沙洋县| 城固县| 洪泽县|