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

首頁 > 系統 > Android > 正文

Android自定義簡單的頂部標題欄

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

本文實例為大家分享了Android實現簡單頂部標題欄的具體代碼,供大家參考,具體內容如下

實現功能:

1)自定義View標題欄布局;

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

3)相對于我之前寫過的一篇,免繼承,可直接在布局里使用

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

老規矩,上幾張效果圖:

Android,標題欄

由效果圖可見,這個是可以根據傳入type來控制,比較靈活的

下面就來實現以下步驟,最后我會貼上源碼

1.創建一個布局文件,命名,layout_titlebar,來部署我們的標題欄樣式,可以自定義更改,圖片文件可暫時用自己的替代

<?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="標題"    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 * * 一個簡單的自定義標題欄 */ 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    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);//標題欄類型,默認為10     //賦值進去我們的標題欄    tvTitle.setText(title);    ivBack.setImageResource(leftIcon);    tvMore.setText(rightText);    ivMore.setImageResource(rightIcon);     //可以傳入type值,可自定義判斷值    if(titleBarType == 10){//不傳入,默認為10,顯示更多 文字,隱藏更多圖標按鈕      ivMore.setVisibility(View.GONE);      tvMore.setVisibility(View.VISIBLE);    }else if(titleBarType == 11){//傳入11,顯示更多圖標按鈕,隱藏更多 文字      tvMore.setVisibility(View.GONE);      ivMore.setVisibility(View.VISIBLE);    }  }   //左邊圖片點擊事件  public void setLeftIconOnClickListener(OnClickListener l){    ivBack.setOnClickListener(l);  }   //右邊圖片點擊事件  public void setRightIconOnClickListener(OnClickListener l){    ivBack.setOnClickListener(l);  }   //右邊文字點擊事件  public void setRightTextOnClickListener(OnClickListener l){    ivBack.setOnClickListener(l);  }}

3.在res下的values下創建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是文字類型,references是圖片類型,integer是數字類型 

4.需要用到我們的這個頂部標題欄的話,就在當前布局引入

可以根據type傳入的值來改變右邊顯示文字還是圖片,可在自定義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="我是標題"    android:layout_width="match_parent"    android:layout_height="wrap_content"></com.titlebar.CustomTitleBar>

5.可以獲取它的id,來調用它的點擊事件

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靈活的自定義頂部標題欄

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


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武山县| 南木林县| 通河县| 湖北省| 威远县| 新兴县| 昌吉市| 玉环县| 临清市| 普安县| 旌德县| 怀远县| 深泽县| 阳原县| 南投市| 湾仔区| 青阳县| 漠河县| 洪江市| 镇康县| 诏安县| 尖扎县| 余姚市| 嘉义县| 五常市| 灵璧县| 阳山县| 仪陇县| 田东县| 合江县| 营口市| 太仓市| 建昌县| 定州市| 界首市| 马关县| 县级市| 纳雍县| 东方市| 湘乡市| 达州市|