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

Android自定義簡(jiǎn)單的頂部標(biāo)題欄

 更新時(shí)間:2018年11月21日 17:13:15   作者:zpf_  
這篇文章主要為大家詳細(xì)介紹了Android自定義簡(jiǎn)單的頂部標(biāo)題欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡(jiǎn)單頂部標(biāo)題欄的具體代碼,供大家參考,具體內(nèi)容如下

實(shí)現(xiàn)功能:

1)自定義View標(biāo)題欄布局;

2)靈活的可以自己傳入類(lèi)型,選擇所需要的控件來(lái)顯示隱藏

3)相對(duì)于我之前寫(xiě)過(guò)的一篇,免繼承,可直接在布局里使用

4)直接可以在布局控件里設(shè)置屬性

老規(guī)矩,上幾張效果圖:

由效果圖可見(jiàn),這個(gè)是可以根據(jù)傳入type來(lái)控制,比較靈活的

下面就來(lái)實(shí)現(xiàn)以下步驟,最后我會(huì)貼上源碼

1.創(chuàng)建一個(gè)布局文件,命名,layout_titlebar,來(lái)部署我們的標(biāo)題欄樣式,可以自定義更改,圖片文件可暫時(shí)用自己的替代

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="50dp">
 
  <ImageView
    android:id="@+id/iv_back"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_marginLeft="20dp"
    android:src="@drawable/icon_back"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 
  <TextView
    android:id="@+id/tv_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="標(biāo)題"
    android:textColor="#000"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 
  <TextView
    android:id="@+id/tv_more"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="更多"
    android:textColor="#000"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 
  <ImageView
    android:id="@+id/iv_more"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:src="@drawable/icon_more"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 
</android.support.constraint.ConstraintLayout>

2.自定義View,繼承自RelativeLayout,第3步貼上attr文件

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
/**
 * @Author : 張
 * @Email : manitozhang@foxmail.com
 * @Date : 2018/9/19
 *
 * 一個(gè)簡(jiǎn)單的自定義標(biāo)題欄
 */
 
public class CustomTitleBar extends RelativeLayout {
 
  private ImageView ivBack;
  private TextView tvTitle;
  private TextView tvMore;
  private ImageView ivMore;
 
  public CustomTitleBar(Context context, AttributeSet attrs) {
    super(context, attrs);
 
    initView(context,attrs);
  }
 
  //初始化視圖
  private void initView(final Context context, AttributeSet attributeSet) {
    View inflate = LayoutInflater.from(context).inflate(R.layout.layout_titlebar, this);
    ivBack = inflate.findViewById(R.id.iv_back);
    tvTitle = inflate.findViewById(R.id.tv_title);
    tvMore = inflate.findViewById(R.id.tv_more);
    ivMore = inflate.findViewById(R.id.iv_more);
 
    init(context,attributeSet);
  }
 
  //初始化資源文件
  public void init(Context context, AttributeSet attributeSet){
    TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.CustomTitleBar);
    String title = typedArray.getString(R.styleable.CustomTitleBar_title);//標(biāo)題
    int leftIcon = typedArray.getResourceId(R.styleable.CustomTitleBar_left_icon, R.drawable.icon_back);//左邊圖片
    int rightIcon = typedArray.getResourceId(R.styleable.CustomTitleBar_right_icon, R.drawable.icon_more);//右邊圖片
    String rightText = typedArray.getString(R.styleable.CustomTitleBar_right_text);//右邊文字
    int titleBarType = typedArray.getInt(R.styleable.CustomTitleBar_titlebar_type, 10);//標(biāo)題欄類(lèi)型,默認(rèn)為10
 
    //賦值進(jìn)去我們的標(biāo)題欄
    tvTitle.setText(title);
    ivBack.setImageResource(leftIcon);
    tvMore.setText(rightText);
    ivMore.setImageResource(rightIcon);
 
    //可以傳入type值,可自定義判斷值
    if(titleBarType == 10){//不傳入,默認(rèn)為10,顯示更多 文字,隱藏更多圖標(biāo)按鈕
      ivMore.setVisibility(View.GONE);
      tvMore.setVisibility(View.VISIBLE);
    }else if(titleBarType == 11){//傳入11,顯示更多圖標(biāo)按鈕,隱藏更多 文字
      tvMore.setVisibility(View.GONE);
      ivMore.setVisibility(View.VISIBLE);
    }
  }
 
  //左邊圖片點(diǎn)擊事件
  public void setLeftIconOnClickListener(OnClickListener l){
    ivBack.setOnClickListener(l);
  }
 
  //右邊圖片點(diǎn)擊事件
  public void setRightIconOnClickListener(OnClickListener l){
    ivBack.setOnClickListener(l);
  }
 
  //右邊文字點(diǎn)擊事件
  public void setRightTextOnClickListener(OnClickListener l){
    ivBack.setOnClickListener(l);
  }
}

3.在res下的values下創(chuàng)建attr文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
 
  <declare-styleable name="CustomTitleBar">
    <attr name="title" format="string"/>
    <attr name="left_icon" format="reference"/>
    <attr name="right_icon" format="reference"/>
    <attr name="right_text" format="string"/>
    <attr name="titlebar_type" format="integer"/>
  </declare-styleable>
 
</resources>

String是文字類(lèi)型,references是圖片類(lèi)型,integer是數(shù)字類(lèi)型 

4.需要用到我們的這個(gè)頂部標(biāo)題欄的話,就在當(dāng)前布局引入

可以根據(jù)type傳入的值來(lái)改變右邊顯示文字還是圖片,可在自定義View自定義該type值

<com.titlebar.CustomTitleBar
    android:id="@+id/titlebar"
    android:background="#DCDCDC"
    app:right_icon="@drawable/icon_more"
    app:right_text="更多"
    app:titlebar_type="11"
    app:left_icon="@drawable/icon_back"
    app:title="我是標(biāo)題"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></com.titlebar.CustomTitleBar>

5.可以獲取它的id,來(lái)調(diào)用它的點(diǎn)擊事件

CustomTitleBar titleBar = findViewById(R.id.titlebar);
    titleBar.setLeftIconOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Toast.makeText(MainActivity.this, "左邊", Toast.LENGTH_SHORT).show();
      }
    });

6.就這么多了,在這里貼上源碼,小伙伴可以試試

Android靈活的自定義頂部標(biāo)題欄

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android?藍(lán)牙BLE開(kāi)發(fā)完全指南

    Android?藍(lán)牙BLE開(kāi)發(fā)完全指南

    BLE藍(lán)牙的興起主要因?yàn)榻陙?lái)可穿戴設(shè)備的流行,由于傳統(tǒng)藍(lán)牙功耗高不能滿足可穿戴設(shè)備對(duì)于續(xù)航的要求,所以大部分可穿戴設(shè)備采用藍(lán)牙4.0,即BLE藍(lán)牙技術(shù),這篇文章主要給大家介紹了關(guān)于Android?藍(lán)牙BLE開(kāi)發(fā)的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • Jetpack?Compose常用組件詳細(xì)介紹

    Jetpack?Compose常用組件詳細(xì)介紹

    本篇開(kāi)始介紹Jetpack?Compose?中常用的組件。有一部分之前的文章中也出現(xiàn)過(guò),今天詳細(xì)說(shuō)明一下
    2022-10-10
  • Android GPS獲取當(dāng)前經(jīng)緯度坐標(biāo)

    Android GPS獲取當(dāng)前經(jīng)緯度坐標(biāo)

    這篇文章主要為大家詳細(xì)介紹了Android GPS獲取當(dāng)前經(jīng)緯度坐標(biāo),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Android實(shí)現(xiàn)帶有刪除按鈕的EditText示例代碼

    Android實(shí)現(xiàn)帶有刪除按鈕的EditText示例代碼

    本文給大家介紹一個(gè)很實(shí)用的小控件,就是在Android系統(tǒng)的輸入框右邊加入一個(gè)小圖標(biāo),點(diǎn)擊小圖標(biāo)可以清除輸入框里面的內(nèi)容,IOS上面直接設(shè)置某個(gè)屬性就可以實(shí)現(xiàn)這一功能,但是Android原生EditText不具備此功能,所以要想實(shí)現(xiàn)這一功能我們需要重寫(xiě)EditText。下面來(lái)看看吧。
    2016-12-12
  • Qt qml中l(wèi)istview 列表視圖控件(下拉刷新、上拉分頁(yè)、滾動(dòng)軸)

    Qt qml中l(wèi)istview 列表視圖控件(下拉刷新、上拉分頁(yè)、滾動(dòng)軸)

    這篇文章主要介紹了Qt qml中l(wèi)istview 列表視圖控件(下拉刷新、上拉分頁(yè)、滾動(dòng)軸) 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • Android編程之創(chuàng)建自己的內(nèi)容提供器實(shí)現(xiàn)方法

    Android編程之創(chuàng)建自己的內(nèi)容提供器實(shí)現(xiàn)方法

    這篇文章主要介紹了Android編程之創(chuàng)建自己的內(nèi)容提供器實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式分析了Android創(chuàng)建內(nèi)容提供器的原理、步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • Android簡(jiǎn)單實(shí)現(xiàn)畫(huà)圖功能

    Android簡(jiǎn)單實(shí)現(xiàn)畫(huà)圖功能

    這篇文章主要為大家詳細(xì)介紹了Android簡(jiǎn)單實(shí)現(xiàn)畫(huà)圖功能的方法,以及實(shí)現(xiàn)過(guò)程中遇到的問(wèn)題,感興趣的小伙伴們可以參考一下
    2016-03-03
  • AndroidStudio代碼達(dá)到指定字符長(zhǎng)度時(shí)自動(dòng)換行實(shí)例

    AndroidStudio代碼達(dá)到指定字符長(zhǎng)度時(shí)自動(dòng)換行實(shí)例

    這篇文章主要介紹了AndroidStudio代碼達(dá)到指定字符長(zhǎng)度時(shí)自動(dòng)換行實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • Android獲取手機(jī)通訊錄、sim卡聯(lián)系人及調(diào)用撥號(hào)界面方法

    Android獲取手機(jī)通訊錄、sim卡聯(lián)系人及調(diào)用撥號(hào)界面方法

    這篇文章主要介紹了Android獲取手機(jī)通訊錄、sim卡聯(lián)系人及調(diào)用撥號(hào)界面方法,本文分別給出實(shí)現(xiàn)代碼實(shí)現(xiàn)獲取通訊錄和sim卡的聯(lián)系人,以及權(quán)限配置和調(diào)用系統(tǒng)撥打電話的界面的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-04-04
  • Retrofit 源碼分析初探

    Retrofit 源碼分析初探

    這篇文章主要介紹了Retrofit 源碼分析初探,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05

最新評(píng)論

凭祥市| 元谋县| 虎林市| 孟津县| 丹棱县| 富蕴县| 家居| 乐清市| 西盟| 民丰县| 平原县| 贵港市| 长汀县| 贵港市| 射洪县| 吐鲁番市| 平顶山市| 鹤庆县| 石嘴山市| 密山市| 庆阳市| 句容市| 郸城县| 遂昌县| 望江县| 都安| 大埔区| 西青区| 改则县| 恩平市| 邯郸市| 鲁山县| 酉阳| 甘洛县| 芮城县| 六枝特区| 芦山县| 彭水| 金门县| 临澧县| 漳浦县|