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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

【Android】实现自定义标题栏

發布時間:2023/12/20 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android】实现自定义标题栏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

不知不覺標題自定義標題欄這個功能已經寫了有將近半年的時間了,總共花了一個來月的下班時間才將其完成,完全靠自己的開發的確有點難,但,說實在的也不難,當初覺得難是因為自己對自定義的View點擊事件一竅不通,翻了許多博客,遲遲未找到處理方法,直到后面腦子換了種思路去思考這個問題,才將其處理掉。

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


標題欄隱藏了,好,我們正式開始進入

新建一個class繼承一個相對布局并重寫其構造方法

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);} }

創建一個Layout文件用于CustomTitleBlock.class綁定并初始化相關控件

<?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文件夾下創建一個attrs.xml文件用于聲明控件的屬性

<?xml version="1.0" encoding="utf-8"?> <resources><declare-styleable name="CustomTitleBlock"><!-- 標題欄左邊 --><attr name="leftText" format="string"/><attr name="leftTextColor" format="color"/><attr name="leftTextSize" format="integer"/><attr name="leftImage" format="reference"/><!-- 標題欄中間 --><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><!-- 標題欄右邊 --><attr name="rightText" format="string"/><attr name="rightTextColor" format="color"/><attr name="rightTextSize" format="integer"/><attr name="rightImage" format="reference"/></declare-styleable> </resources>

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

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

因為我設置的declare-styleable屬性不止一條,需要使用循環來進行遍歷,通過遍歷來獲得相對應的屬性

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

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

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


光有效果還不行,觸發事件還沒有,無疑是擺設,自定義的控件點擊事件并不難,聲明幾個公共方法去實現即可

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

至此,自定義標題欄已完成,有不懂的可以下載文件觀看哦-點擊下載本文代碼

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。