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

时间:2017-06-26 14:13:06

标签: android android-actionbar

我目前正在开发一个聊天应用程序,我希望聊天活动标题可以点击,以便我可以开始另一个活动。我已经使用了自定义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" />

但是,这与本机操作栏的样式不匹配,因为波纹动画颜色与其他导航按钮不同:

enter image description here
这就是原生按钮的外观。

enter image description here
这就是我的文字在点击时的样子。

如何将其更改为本机UI?

1 个答案:

答案 0 :(得分:1)

纹波大小

自API 23起,动作按钮上的涟漪直径为40dp。将其添加到TextView

android:background="?selectableItemBackgroundBorderless"

删除style属性。不要在不理解其含义的情况下滥用风格,这显然不是导航按钮。

波纹颜色

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

选项a)

您可以直接在XML布局文件中的TextView中使用Toolbar

选项b)

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

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"

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