首页 文章

Android工具栏颜色未由colorPrimary设置

提问于
浏览
5

根据谷歌文档,我应该能够在主题中使用colorPrimary设置工具栏背景的颜色,但它不起作用 . 这就是我所拥有的:

styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/light_purple</item>
        <!-- colorPrimaryDark is used for the status bar -->
        <item name="colorPrimaryDark">@color/dark_purple</item>
        <!-- colorAccent is used as the default value for colorControlActivated,
             which is used to tint widgets -->
        <item name="colorAccent">@color/dark_purple</item>

        <item name="colorSwitchThumbNormal">@color/light_purple</item>
    </style>

</resources>

活动布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/AppTheme"
        tools:showIn="@layout/activity_main">
        <TextView
            android:id="@+id/pivot_title_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="toolbar text view" />
    </android.support.v7.widget.Toolbar>
...
</LinearLayout>

活动:

public class MainActivity extends AppCompatActivity {

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

我在清单中将我的应用主题设置为AppTheme: android:theme="@style/AppTheme" > 我在build.gradle中设置了android支持appcompat

compile 'com.android.support:appcompat-v7:22.1.0'

但是我的工具栏仍然没有着色 . I know I can manually set the toolbar background color manually in the layout file ,但不应该从主题中获得它的颜色?正如你所看到的强调颜色正在起作用 .

enter image description here

4 回答

  • 6

    工具栏不会从主题中获得原色 . 您必须设置工具栏的以下xml属性

    android:background="@color/primary"
    

    这是我工作的工具栏实现 .

    <?xml version="1.0" encoding="utf-8"?>
        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/primary"
            android:minHeight="?actionBarSize">
    </android.support.v7.widget.Toolbar>
    

    希望它也适合你 .

  • 5

    您可以将样式应用于 Toolbar

    Style and theme are different.

    样式为工具栏视图 local ,例如背景颜色 .

    对于工具栏中充气的所有ui元素,主题是 global ,例如 Headers 和图标的颜色 .

    更多信息here .

    <android.support.v7.widget.Toolbar
           style="@style/HeaderBar"/>
    

    哪里:

    <style name="HeaderBar">
        <item name="android:background">?colorPrimary</item>
    </style>
    

    否则,您可以定义工具栏的背景 .

    <android.support.v7.widget.Toolbar 
      android:background="?attr/colorPrimary">
    
  • 4

    你必须在android 5的主题中使用 android: 前缀属性,因为非前缀变体仅适用于版本<android 5的app compat部分 .

    所以你应该有一个 values/styles.xml 用于pre-android 5和一个 values-v21/styles.xml 用于android 5 .

    在v21样式中,您可以按如下方式定义主题:

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:colorPrimary">@color/light_purple</item>
        <item name="android:colorPrimaryDark">@color/dark_purple</item>
        <item name="android:colorAccent">@color/dark_purple</item>
    </style>
    

    正如您现在将colorPrimary属性简单地定义为前Lollipop的 colorPrimary 和Lollipop设备的 android:colorPrimary 一样,您不能再直接使用 ?attr/colorPrimary . 相反,像之前所说的其他人一样,您应该为工具栏定义自己的样式,但对于这两种变体:

    Value 观/ styles.xml:

    <style name="Toolbar">
      <item name="android:background">?attr/colorPrimary</item>
    </style>
    

    值-V21 / styles.xml:

    <style name="Toolbar">
      <item name="android:background">?android:attr/colorPrimary</item>
    </style>
    

    并使用工具栏的样式:

    <android.support.v7.widget.Toolbar
        style="@style/Toolbar"
    

    像这样,可以在所有版本上设置背景颜色和其他样式,并且仍然可以通过主题中设置的 colorPrimary 进行更改 .

  • 0

    尝试将背景颜色设置为工具栏

    这对我有用

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_main"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:background="@color/primary_color"
            android:gravity="right"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <ProgressBar
                android:id="@+id/pb_loading_news"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:visibility="gone" />
        </android.support.v7.widget.Toolbar>
    

相关问题