关于Android:Android-?attr / colorPrimary无法正常工作

Android - ?attr/colorPrimary not working

我的问题是一个奇怪的问题(我认为)。

使用AppCompat我对?attr / colorPrimary的引用不起作用。

colors.xml:

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">@color/primary_material_dark</color>
    <color name="colorPrimaryDark">@color/primary_dark_material_dark</color>
    <color name="test">#ff2800</color>
</resources>

styles.xml:

1
2
3
4
5
6
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:colorPrimary">@color/primary_material_dark</item>
        <item name="android:colorPrimaryDark">@color/primary_dark_material_dark</item>
    </style>
</resources>

activity_main.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          tools:context=".MainActivity"
          android:background="?attr/colorPrimary">

    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

        <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

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

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary">

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="@string/hello_world"/>
        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="COLORCLOR"
        android:background="?attr/colorPrimary"/>
    </LinearLayout>


</LinearLayout>

这里是什么样子:

?attr/colorPrimary

但是当我用实际的颜色替换替换?attr / colorPrimary引用时,一切似乎都正常工作。对此很困惑,尝试从工具栏中删除主题和popupTheme属性,仍然无法正常工作。

PS。 ?attr / colorPrimaryDark工作得很好


从样式中删除android标签,以使您能够使用材质设计主题:

1
2
3
4
<style name="AppTheme"  parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary_material_dark</item>
    <item name="colorPrimaryDark">@color/primary_dark_material_dark</item>
</style>


dependencies {}

部分的build.gradle(Module:app)中添加implementation 'com.android.support:design:26.1.0'

1
2
3
4
5
6
7
8
9
10
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    // If I  commented I have your error
    implementation 'com.android.support:design:26.1.0'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

棒棒糖上存在问题。您可以更改

1
android:background="?attr/colorPrimary"

而不是

1
app:background="?attr/colorPrimary"

它将正常工作。


我遇到了同样的问题,那么我找到了删除此行所需的解决方法

1
<item name="android:statusBarColor">@android:color/transparent</item>

它覆盖了colorPrimaryDark颜色,并且基本上从状态栏中删除了它
style.xml文件


在工具栏中尝试

1
android:background="?android:attr/colorPrimary"

和样式:

1
<item name="colorPrimary">@color/primary</item>


在gradle中更新到最新的库。单击红色灯泡进行更新。

1
implementation 'com.android.support:design:27.1.1'

如果使用的是27.1.1,请确保所有其他库也使用的是27.1.1。