更改工具栏中标题的颜色和大小- Android

Change color and size of the title in Toolbar- Android

我正在尝试更改工具栏的标题颜色和大小,但没有任何反应。我到处搜索,似乎没有任何效果。我正在使用 minsdk 19 api。这是我的代码:

**activity.main.xml**

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        />

</android.support.design.widget.AppBarLayout>

**style.xml**

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/primaryColor</item>
    <item name="colorPrimaryDark">@color/primaryDarkColor</item>
    <!--<item name="colorAccent">@color/primaryLightColor</item>-->
    <item name="android:textColor">@color/primaryTextColor</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">

    <item name="actionBarStyle">@style/MyTitleTextStyle</item>

</style>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />


<style name="MyTitleTextStyle" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textColor">@color/primaryColor</item>
</style>

</resources>

解释得很好。将此添加到您的工具栏:

app:titleTextAppearance="@style/Toolbar.TitleText"

并为其创建样式:

<style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textColor">#FFFFFF</item>
</style>

你也可以试试这个。这将允许您控制 textsize

style.xml

中定义ActionBarTextAppearance
  <style name="ActionBarTextAppearance" parent="@android:style/TextAppearance.Medium">
        <item name="android:textSize">30sp</item>
        <item name="android:textColor">#ec1212</item>
        <item name="android:textStyle">bold</item>
  </style>

然后在main.xml

中使用
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay"
    app:titleTextAppearance="@style/ActionBarTextAppearance"
  />

实现目标的简单方法是将文本视图作为工具栏中的标题,然后提供您选择的大小和颜色

 <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        >
<Textview 
  android:layout_width="wrap_parent"
 android:layout_height="wrap_parent"/>


</android.support.v7.widget.Toolbar>