[英]Custom action bar button style color issue
我目前正在開發一個聊天應用程序,並且希望可以單擊聊天活動標題,以便我可以開始其他活動。 我已經使用自定義ActionBar和以下布局代碼進行了此操作:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Widget.AppCompat.Toolbar.Button.Navigation"
android:id="@+id/actionbarTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:text="@string/app_name"
android:textColor="#FFFFFF"
android:textSize="20sp" />
但是,這與本機操作欄的樣式不匹配,因為波紋動畫的顏色與其他導航按鈕不同:
如何將其更改為本機UI?
自API 23起,操作按鈕上的波紋直徑為40dp。將其添加到TextView
:
android:background="?selectableItemBackgroundBorderless"
刪除style
屬性。 不要在不理解其含義的情況下濫用樣式,這顯然不是導航按鈕。
擴展視圖時,您需要獲得最中間的上下文,以便正確解析主題屬性(例如?selectableItemBackgroundBorderless
。
選項a)
您可以直接在XML布局文件的Toolbar
TextView
。
選項b)
當添加TextView
手動添加到工具欄使用LayoutInflater
從該Toolbar
。
val context = toolbar.context
val inflater = LayoutInflater.from(context)
選項c)
將此TextView
設置為ActionBar
自定義視圖時,請使用Action Bar的主題上下文。
val context = supportActionBar!!.themedContext
val inflater = LayoutInflater.from(context)
更換
android:textColor="#FFFFFF"
android:textSize="20sp"
與
android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
用於從主題繼承的標准字體大小和文本顏色,以防您開始使用燈光操作欄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.