i++

プログラム系のメモ書きなど

Android : Toolbar のスタイル(背景とテキスト)を変える

背景色は Toolbar の android:background で、テキストの色とシステムアイコン(オーバーフローアイコンやハンバーガーボタン)の色は app:theme で変更します。Application 本体の Theme から変更する場合に使います。

テキストのスタイルを細かく変える場合は app:titleTextAppearance を使います(標準的にするなら基本弄らないはずですが)。

layout xml での Toolbar の定義

xmlns:app="http://schemas.android.com/apk/res-auto" を定義済みとしています。

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:background="?attr/colorPrimary"
    app:theme="@style/ToolbarTheme"
    app:titleTextAppearance="@style/ToolbarTextAppearance"/>
styles.xml
<style name="ToolbarTheme" parent="Theme.AppCompat.NoActionBar">
    <!--タイトルテキストの色-->
    <item name="android:textColorPrimary">@color/white_primary_text</item>
    <!--ハンバーガーボタンなどのアイコンの色-->
    <item name="android:textColorSecondary">@color/white</item>
</style>

<style name="ToolbarTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
    <!--テキスト関係を変えられる。これはただの例。-->
    <item name="android:textSize">8dp</item>
</style>