自定义操作栏按钮样式颜色问题

Custom action bar button style color issue

我目前正在开发一个聊天应用程序,我希望聊天 activity header 可以点击,这样我就可以开始另一个 activity。我已经使用自定义 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?

波纹大小

动作按钮上的波纹直径为 40dp,因为 API 23。将此添加到 TextView

android:background="?selectableItemBackgroundBorderless"

删除 style 属性。不要在不了解含义的情况下滥用样式,这显然不是导航按钮。

波纹颜色

您需要在膨胀视图时获取最中间的上下文,以便正确解析 ?selectableItemBackgroundBorderless 等主题属性。

选项 a)

您可以直接在 XML 布局文件中的 Toolbar 中添加 TextView

选项 b)

手动将 TextView 添加到工具栏时,使用 Toolbar 中的 LayoutInflater

val context = toolbar.context
val inflater = LayoutInflater.from(context)

选项 c)

将此 TextView 设置为 ActionBar 自定义视图时,使用操作栏的主题上下文。

val context = supportActionBar!!.themedContext
val inflater = LayoutInflater.from(context)

文字颜色

替换

android:textColor="#FFFFFF"
android:textSize="20sp"

android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"

用于从主题继承的标准字体大小和文本颜色,以防您开始使用轻型操作栏。