我的问题很奇怪(我想).
使用AppCompat我对?attr/colorPrimary的引用不起作用.
colors.xml:
<?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>
Run Code Online (Sandbox Code Playgroud)
styles.xml:
<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>
Run Code Online (Sandbox Code Playgroud)
activity_main.xml中:
<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>
Run Code Online (Sandbox Code Playgroud)
看到它的样子:
但是当我用实际的颜色参考替换?attr/colorPrimary引用时,一切似乎都能正常工作.真的很困惑这个,尝试从工具栏中删除主题和popupTheme属性,仍然无法正常工作.
PS.?attr/colorPrimaryDark工作得很好
Ang*_*i H 12
android从您的样式中删除标记,以使您能够使用材质设计主题:
<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>
Run Code Online (Sandbox Code Playgroud)
添加implementation 'com.android.support:design:26.1.0'在的build.gradle(模块:应用)在第dependencies {}
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'
}
Run Code Online (Sandbox Code Playgroud)