修改android Toolbar的标题大小和按钮图标颜色

使用android toolbar的时候,toolbar中的标题、二级标题以及按钮的图标的颜色都会使用默认的值。但是,有时候我们必须要自定义它们的大小以及颜色,该如何自定义呢?

解决方法:

(1)修改标题、二级标题的字体大小和颜色,可以通过style进行修改,然后在引用相对应的定义好的style样式。

style样式,例如:

<style name="Theme.ToolBar.Base.Title" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">@android:color/white</item>
</style>
<style name="Theme.ToolBar.Base.Subtitle" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
    <item name="android:textSize">11sp</item>
    <item name="android:textColor">@color/toolbar_subTile_color</item>
</style>

引用定义好的样式:

1)在java代码中引用:

mToolbar.setTitleTextAppearance(this,R.style.Theme_ToolBar_Base_Title);
mToolbar.setSubtitleTextAppearance(this,R.style.Theme_ToolBar_Base_Subtitle);

2)在xml中引用:

app:titleTextAppearance="@style/Theme.ToolBar.Base.Title"
app:subtitleTextAppearance="@style/Theme.ToolBar.Base.Subtitle"

(2)修改toolbar中按钮图标的颜色。同样通过定义style样式修改按钮图标的颜色,最后通过theme属性引用定义好的样式。

style样式,例如:

<style name="MyToolbarTheme" parent="ThemeOverlay.AppCompat.ActionBar">
    <item name="colorControlNormal">#ffffff</item><!-- 修改toolbar中按钮图标的颜色 -->
</style>

引用style样式:

app:theme="@style/MyToolbarTheme"

猜你喜欢

转载自my.oschina.net/u/2002921/blog/709363
今日推荐