Android修改Toolbar标题和菜单项字体大小和颜色

修改Toolbar标题字体大小

首先我们要写一个style:


<style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textSize">18spitem>
style>

然后在布局xml中使用:

<android.support.v7.widget.Toolbar
            xxxxxxxxxxxxxxxxxxx
            app:titleTextAppearance="@style/Toolbar.TitleText" />

修改菜单项字体大小和颜色

我在StackOverFlow上找到一个回答,可以参考一下:
Change Toolbar Menu Item color (non-hidden action)

首先我们要写一个style:


<style name="ToolbarTheme" parent="AppTheme.AppBarOverlay">
    <item name="actionMenuTextColor">@color/edit_green 
    "android:textSize">@dimen/toolbar_action_menu_text 
style>

然后在toolbar布局xml中使用:

<android.support.v7.widget.Toolbar
            xxxxxxxxxxx
            app:theme="@style/ToolbarTheme"/>

这里对于改变字体大小没有什么好说的,但是改颜色有两个坑需要注意一下:
①上边style中item actionMenuTextColor前边是不能加android:的,加上之后就不行了!
②另外style中的父style也比较重要,一般写AppTheme.AppBarOverlay就可以了,写其他的要么就是颜色改变不了,要么就是整个toolbar深浅主题变了,导致toolbar中的标题也变了颜色,当然是在你没有设置标题颜色的前提下。因为Android默认会根据你主题的深浅,来设置默认的标题颜色,深主题就会设置一个浅色标题,浅主题就会设置一个深色标题。

经过测试,在5.0及以上,还有4.0的机子上均可正常改变颜色。
至于网上有些说要在style-v21中也写,而且要加上android:,完全没有必要!

TabLayout改变TabText的字体大小

这个跟改变Toolbar标题字体大小类似,也在这提一下。
首先写一个style:

<style name="TabText" parent="TextAppearance.Design.Tab">
    <item name="android:textSize">@dimen/tab_indicator_text
style>

然后在布局中使用:

<android.support.design.widget.TabLayout
            android:id="@+id/tablayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/tab_select_color"
            app:tabIndicatorHeight="@dimen/tab_indicator_height"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@color/white"
            app:tabTextAppearance="@style/TabText"  //改变大小
            app:tabTextColor="@color/white" />

你可能感兴趣的:(Android学习笔记)