日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

【Android】实现自定义标题栏

發(fā)布時(shí)間:2023/12/20 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android】实现自定义标题栏 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

不知不覺(jué)標(biāo)題自定義標(biāo)題欄這個(gè)功能已經(jīng)寫(xiě)了有將近半年的時(shí)間了,總共花了一個(gè)來(lái)月的下班時(shí)間才將其完成,完全靠自己的開(kāi)發(fā)的確有點(diǎn)難,但,說(shuō)實(shí)在的也不難,當(dāng)初覺(jué)得難是因?yàn)樽约簩?duì)自定義的View點(diǎn)擊事件一竅不通,翻了許多博客,遲遲未找到處理方法,直到后面腦子換了種思路去思考這個(gè)問(wèn)題,才將其處理掉。

自定義標(biāo)題欄,就要隱藏掉自帶的標(biāo)題欄,隱藏標(biāo)題欄只需要在AndroidManifest.xml文件的application標(biāo)簽theme的引用樣式的父樣式更換為有NoActionBar字樣的即可


標(biāo)題欄隱藏了,好,我們正式開(kāi)始進(jìn)入

新建一個(gè)class繼承一個(gè)相對(duì)布局并重寫(xiě)其構(gòu)造方法

public class CustomTitleBlock extends RelativeLayout {public CustomTitleBlock(Context context) {super(context);}public CustomTitleBlock(Context context, AttributeSet attrs) {super(context, attrs);}public CustomTitleBlock(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public CustomTitleBlock(Context context, AttributeSet attrs, int defStyleAttr,int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);} }

創(chuàng)建一個(gè)Layout文件用于CustomTitleBlock.class綁定并初始化相關(guān)控件

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/layout_title"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white"><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginLeft="12dp"android:layout_marginRight="12dp"><androidx.constraintlayout.widget.ConstraintLayoutandroid:id="@+id/left"android:layout_width="wrap_content"android:layout_height="match_parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="parent"><ImageViewandroid:id="@+id/iv_left"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/tv_left"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toRightOf="@+id/iv_left"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout><TextViewandroid:id="@+id/tv_centent"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textStyle="bold"android:textSize="16sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintLeft_toLeftOf="parent"/><EditTextandroid:id="@+id/search_box"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_marginTop="4dp"android:layout_marginBottom="4dp"android:textSize="14sp"android:background="@drawable/search_box_style"android:drawableLeft="@drawable/search_64"android:drawablePadding="6dp"android:paddingLeft="8dp"android:paddingRight="8dp"android:visibility="invisible"app:layout_constraintLeft_toRightOf="@+id/left"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintRight_toLeftOf="@+id/right"/><androidx.constraintlayout.widget.ConstraintLayoutandroid:id="@+id/right"android:layout_width="wrap_content"android:layout_height="match_parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintRight_toRightOf="parent"><TextViewandroid:id="@+id/tv_right"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintRight_toLeftOf="@+id/iv_right"/><ImageViewandroid:id="@+id/iv_right"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toBottomOf="parent"/></androidx.constraintlayout.widget.ConstraintLayout></androidx.constraintlayout.widget.ConstraintLayout></RelativeLayout>

在values文件夾下創(chuàng)建一個(gè)attrs.xml文件用于聲明控件的屬性

<?xml version="1.0" encoding="utf-8"?> <resources><declare-styleable name="CustomTitleBlock"><!-- 標(biāo)題欄左邊 --><attr name="leftText" format="string"/><attr name="leftTextColor" format="color"/><attr name="leftTextSize" format="integer"/><attr name="leftImage" format="reference"/><!-- 標(biāo)題欄中間 --><attr name="centerText" format="string"/><attr name="centerTextColor" format="color"/><attr name="centerTextSize" format="integer"/><!-- 搜索框 --><attr name="searchBox" format="boolean"/><attr name="searchBoxHint" format="string"/><attr name="searchBoxWidth" format="dimension"><enum name="match_parent" value="-1"/><enum name="wrap_content" value="-2"/></attr><!-- 標(biāo)題欄右邊 --><attr name="rightText" format="string"/><attr name="rightTextColor" format="color"/><attr name="rightTextSize" format="integer"/><attr name="rightImage" format="reference"/></declare-styleable> </resources>

接下來(lái)我們可以通過(guò)Context.obtainStyledAttributes方法綁定attrs文件的declare-styleable屬性,返回的TypedArray可以獲得declare-styleable屬性的索引

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleBlock, defStyleAttr, 0);

因?yàn)槲以O(shè)置的declare-styleable屬性不止一條,需要使用循環(huán)來(lái)進(jìn)行遍歷,通過(guò)遍歷來(lái)獲得相對(duì)應(yīng)的屬性

for (int i = 0; i < count; i++) {int attr = typedArray.getIndex(i);switch (attr) {case R.styleable.CustomTitleBlock_leftText:// 此處寫(xiě)該屬性賦值后的邏輯應(yīng)該進(jìn)行什么樣的操作break;case R.styleable.CustomTitleBlock_leftTextColor:// 此處寫(xiě)該屬性賦值后的邏輯應(yīng)該進(jìn)行什么樣的操作break;} } // 循環(huán)完要記得回收 typedArray.recycle();

其中,R.styleable.CustomTitleBlock_leftText 中,下劃線前面指的是在values目錄下創(chuàng)建的attrs文件里面的 declare-styleable ,下劃線后面指的是該 declare-styleable 下的 attr
還需要在layout文件引用就可以看見(jiàn)效果啦

<com.demo.maininterface.view.CustomTitleBlockandroid:layout_width="match_parent"android:layout_height="46dp"app:leftImage="@drawable/return_left"app:centerText="@string/bluetooth"/>


光有效果還不行,觸發(fā)事件還沒(méi)有,無(wú)疑是擺設(shè),自定義的控件點(diǎn)擊事件并不難,聲明幾個(gè)公共方法去實(shí)現(xiàn)即可

public void setOnLeftClickListener(OnClickListener onClickListener){left.setOnClickListener(onClickListener);}public void setOnRightClickListener(OnClickListener onClickListener){right.setOnClickListener(onClickListener);}

至此,自定義標(biāo)題欄已完成,有不懂的可以下載文件觀看哦-點(diǎn)擊下載本文代碼

總結(jié)

以上是生活随笔為你收集整理的【Android】实现自定义标题栏的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。