2016-03-08 151 views
0


我嘗試在Pre-Lollipop設備上運行我的應用程序。但是,這段代碼給我展示了Pre-Lolliop和棒棒糖設備的相同錯誤。
如果我將「android:」添加到樣式21中的樣式中,棒棒糖設備可以正常工作,但會顯示一些像android:color要求Android lvl 21(當前Api lvl爲17)的錯誤。用於前棒棒糖和棒棒糖設備的材料設計顏色不起作用

風格

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="AppTheme.Base" parent="@style/Theme.AppCompat.Light.NoActionBar"> 
     <item name="colorPrimary">@color/primary</item> 
     <item name="colorPrimaryDark">@color/primary_dark</item> 
     <item name="colorAccent">@color/accent</item> 
     <item name="textColor">@color/primary_dark</item> 
     <item name="editTextColor">@color/primary_dark</item> 
     <item name="textColorPrimary">@color/icons</item> 
     <item name="imageButtonStyle">@color/icons</item> 
    </style> 

    <style name="MyCustomToolbarTheme" parent="ThemeOverlay.AppCompat.Light"> 
     <item name="textColorPrimary">@color/icons</item> 
     <item name="textColorSecondary">@color/primary_light</item> 
     <item name="colorControlHighlight">@color/icons</item> 
    </style> 
</resources> 

styles21

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="AppTheme" parent="AppTheme.Base"> 
     <item name="android:colorPrimary">@color/primary</item> 
     <item name="android:colorPrimaryDark">@color/primary_dark</item> 
     <item name="android:colorAccent">@color/accent</item> 
     <item name="android:textColor">@color/primary_dark</item> 
     <item name="android:editTextColor">@color/primary_dark</item> 
     <item name="android:textColorPrimary">@color/icons</item> 
     <item name="android:imageButtonStyle">@color/icons</item> 
    </style> 

    <style name="MyCustomToolbarTheme" parent="ThemeOverlay.AppCompat.Light"> 
     <item name="android:textColorPrimary">@color/icons</item> 
     <item name="android:textColorSecondary">@color/primary_light</item> 
     <item name="android:colorControlHighlight">@color/icons</item> 
    </style> 
</resources> 

顏色

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="primary">#4CAF50</color> 
    <color name="primary_dark">#388E3C</color> 
    <color name="primary_light">#C8E6C9</color> 
    <color name="accent">#8BC34A</color> 
    <color name="primary_text">#212121</color> 
    <color name="secondary_text">#727272</color> 
    <color name="icons">#FFFFFF</color> 
    <color name="divider">#B6B6B6</color> 
</resources> 

搖籃

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.2" 

    defaultConfig { 
     applicationId "com.example.hansbaum.calculator" 
     minSdkVersion 17 
     targetSdkVersion 23 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile "com.android.support:appcompat-v7:23.+" 
} 

清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.hansbaum.calculator"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:configChanges="orientation|screenSize" 
      > 

      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 

</manifest> 

錯誤

enter image description here

回答

1

使用Android:colorPrimary。如果您從Theme.AppCompat繼承,colorPrimary將僅適用於它。覆蓋值-v21中的所有主題。

<style name="AppTheme.Base" parent="android:Theme.Material"> 
    <item name="colorPrimary">@color/primary</item> 
    <item name="colorPrimaryDark">@color/primary_dark</item> 
    <item name="colorAccent">@color/accent</item> 
    <item name="textColor">@color/primary_dark</item> 
    <item name="editTextColor">@color/primary_dark</item> 
    <item name="textColorPrimary">@color/icons</item> 
    <item name="imageButtonStyle">@color/icons</item> 
</style> 
+0

謝謝,但不工作。 –

+0

在build.gradle文件 –

+0

中更新buildToolsVersion '23 .0.2'不起作用。始終是相同的錯誤信息 –

0

colorPrimarycolorPrimaryDarkcolorAccent不撒謊的 '機器人' 命名空間。但是,其餘的人躺在android命名空間中。所以,你的風格(正常和v21)應該是這樣的:

<style name="AppTheme.Base" parent="@style/Theme.AppCompat.Light.NoActionBar"> 
     <item name="colorPrimary">@color/primary</item> 
     <item name="colorPrimaryDark">@color/primary_dark</item> 
     <item name="colorAccent">@color/accent</item> 
     <item name="android:textColor">@color/primary_dark</item> 
     <item name="android:editTextColor">@color/primary_dark</item> 
     <item name="android:textColorPrimary">@color/icons</item> 
     <item name="android:imageButtonStyle">@color/icons</item> 
    </style>