为什么应用栏中的文字颜色不会更改?

时间:2018-11-19 08:51:21

标签: android

即使我试图将它们设置为棕色,应用程序栏中的文本和图标的颜色仍为白色。我的风格怎么了?

<style name="HobbesActionBarStyle" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:background">@color/HobbesBackground</item>
    <item name="android:tint">@color/HobbesText</item>
    <item name="colorControlNormal">@color/HobbesText</item>
    <item name="android:subtitleTextStyle">@style/Hobbes.ActionBar.CustomTitle</item>
    <item name="android:titleTextStyle">@style/Hobbes.ActionBar.CustomTitle</item>
    <item name="subtitleTextStyle">@style/Hobbes.ActionBar.CustomTitle</item>
    <item name="titleTextStyle">@style/Hobbes.ActionBar.CustomTitle</item>
</style>


<!-- ActionBar title text -->
<style name="Hobbes.ActionBar.CustomTitle" parent="Widget.AppCompat.ActionBar">
    <item name="android:textColor">@color/HobbesText</item>
    <!-- The textColor property is backward compatible with the Support Library -->
</style>

<style name="HobbesAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/HobbesActionBarStyle</item>
    <item name="actionModeBackground">@color/HobbesBackground</item>
    <item name="colorPrimary">@color/HobbesBackground</item>
    <item name="colorPrimaryDark">@color/black_overlay</item>
    <item name="colorControlNormal">@color/HobbesBackground</item>
    <item name="colorControlActivated">@color/HobbesBackground</item>
    <item name="colorControlHighlight">@color/HobbesBackground</item>
    <item name="colorAccent">@color/HobbesText</item>

</style>

3 个答案:

答案 0 :(得分:1)

像这样创建自定义工具栏:

toolbar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="false"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="@android:color/backgroundColor"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/Base.Theme.AppCompat.Light.DarkActionBar">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/yourCustomColor"/>

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

将其包括在您的布局中

<include layout="@layout/toolbar_layout"/>

在活动的onCreate方法中使用它:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_card);

    Toolbar toolbar = findViewById(R.id.toolbar);
    TextView toolbarTitle = findViewById(R.id.toolbar_title);

    setSupportActionBar(toolbar);
    if (getSupportActionBar() != null)
        getSupportActionBar().setDisplayShowTitleEnabled(false);

    toolbarTitle.setText("Your title");
    ...
}

希望这会有所帮助。

答案 1 :(得分:0)

您必须在样式中添加textColorPrimary

    <item name="android:textColorPrimary">@color/colorRed</item>

<style name="appBarTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:textColorPrimary">@color/colorRed</item>
</style>

答案 2 :(得分:0)

这就是我的做法:

在xml中:

     <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/dark_header">


        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            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"
                android:baselineAligned="false"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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

</RelativeLayout>

在styles.xml中:

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

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

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

在activity.xml中

 <include
    android:id="@+id/header"
    layout="@layout/common_header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

活动中:

private void setToolBar() {
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    final Drawable upArrow = getApplicationContext().getResources().getDrawable(R.drawable.abc_ic_ab_back_material); // this adds the back button remove it if you dont want it
    actionBar.setHomeAsUpIndicator(upArrow);
    actionBar.setDisplayShowTitleEnabled(true);
    toolbar.setTitleTextColor(getColor(R.color.colorAccent));  // this changes the text color
    actionBar.setTitle("title name);
    context = getApplicationContext();
}

希望它会有所帮助:)