Android自定義簡單的頂部標(biāo)題欄
本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡單頂部標(biāo)題欄的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)功能:
1)自定義View標(biāo)題欄布局;
2)靈活的可以自己傳入類型,選擇所需要的控件來顯示隱藏
3)相對(duì)于我之前寫過的一篇,免繼承,可直接在布局里使用
4)直接可以在布局控件里設(shè)置屬性
老規(guī)矩,上幾張效果圖:
由效果圖可見,這個(gè)是可以根據(jù)傳入type來控制,比較靈活的
下面就來實(shí)現(xiàn)以下步驟,最后我會(huì)貼上源碼
1.創(chuàng)建一個(gè)布局文件,命名,layout_titlebar,來部署我們的標(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è)簡單的自定義標(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)題欄類型,默認(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是文字類型,references是圖片類型,integer是數(shù)字類型
4.需要用到我們的這個(gè)頂部標(biāo)題欄的話,就在當(dāng)前布局引入
可以根據(jù)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="我是標(biāo)題" android:layout_width="match_parent" android:layout_height="wrap_content"></com.titlebar.CustomTitleBar>
5.可以獲取它的id,來調(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.就這么多了,在這里貼上源碼,小伙伴可以試試
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
textView 添加超鏈接(兩種實(shí)現(xiàn)方式)
在textView添加超鏈接,有兩種方式,第一種通過HTML格式化你的網(wǎng)址,一種是設(shè)置autolink,讓系統(tǒng)自動(dòng)識(shí)別超鏈接,下面為大家介紹下這兩種方法的實(shí)現(xiàn)2013-06-06Android 短信轉(zhuǎn)換成彩信的消息數(shù)量(實(shí)例代碼)
本文通過實(shí)例代碼給大家介紹了Android 短信轉(zhuǎn)換成彩信的消息數(shù)量,需要的朋友可以參考下2017-05-05Android編程之截屏實(shí)現(xiàn)方法(包括scrollview與listview)
這篇文章主要介紹了Android編程之截屏實(shí)現(xiàn)方法,包括截取scrollview與listview屏幕的相關(guān)技巧,以及截屏圖片的生成與保存技巧,需要的朋友可以參考下2015-11-11Android提高之SQLite分頁表格實(shí)現(xiàn)方法
這篇文章主要介紹了Android提高之SQLite分頁表格實(shí)現(xiàn)方法,在項(xiàng)目開發(fā)中有很高的實(shí)用價(jià)值,需要的朋友可以參考下2014-08-08第三方開源Android TickPlusDrawable狀態(tài)可以通過動(dòng)畫切換的按鈕
Android tickplusdrawable(TickPlusDrawable)是一個(gè)狀態(tài)可以通過動(dòng)畫切換的按鈕,本文給大家分享第三方開源Android TickPlusDrawable狀態(tài)可以通過動(dòng)畫切換的按鈕,感興趣的朋友一起學(xué)習(xí)吧2015-12-12Android Studio下添加assets目錄的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狝ndroid Studio下添加assets目錄的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03Android 使用ViewPager自動(dòng)滾動(dòng)循環(huán)輪播效果
本文主要給大家介紹viewpager自動(dòng)播放,循環(huán)滾動(dòng)的效果,對(duì)android viewpager滾動(dòng)相關(guān)知識(shí)感興趣的朋友可以參考下本篇文章2015-11-11