2016-12-30 29 views
3

首先,我创建了两种样式,一种是value,另一种是value-21。在AppCompatActivity中不能使用android:Theme.Material

在值/ styles.xml是

<resources> 
    <style name="AppTheme" parent="Theme.AppCompat"></style> 
</resources> 

在值-V21/styles.xml是

<resources> 
    <style name="AppTheme" parent="android:Theme.Material"></style> 
</resources> 


我的文件的gradle(app.gradle)

gradle pic1 gradle pic2


由于ActionBarActivity已过时,所以我用AppCompatActivity

当我设置这两个风格价值观和MainActivity延长AppCompatActivity,我得到了异常:

java.lang.IllegalStateException: You need to use a Theme.AppCompat 
theme (or descendant) with this activity. 

我做了两个不同的变化和异常不会再显示出来:

  • 扩展“android.app.Activity”而不是v7.AppCompatActivity。
  • 更改为values-v21/styles.xml中的“Theme.AppCompat”。


我应该延长 “android.app.Activity” 如果我想使用的设计主题?

或者,还有另一种使用材料设计主题的方法吗?

感谢。

+0

的Android L可如果使用AppComatActivity使用Theme.Meterial刚使用Theme.AppCompat – Fang

+0

什么情况会使用“android:Theme.Material”? – cmingmai

+0

minSdkVersion是21 – Fang

回答

0

试试这个:

<resources> 
    <style name="AppTheme" parent="parent="Theme.AppCompat.Light"></style> 
</resources> 
2

替换以下主题在你的styles.xml文件。

<!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <!-- Customize your theme here. --> 
    </style> 

    <style name="MyMaterialTheme" parent="MyMaterialTheme.Base"> 
     <item name="android:windowContentTransitions">true</item> 
     <item name="android:windowAllowEnterTransitionOverlap">true</item> 
     <item name="android:windowAllowReturnTransitionOverlap">true</item> 
    </style> 

    <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <item name="windowNoTitle">true</item> 
     <item name="windowActionBar">false</item> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
    </style> 
</resources> 

并使用您的MainActivity扩展AppCompatActivity。

最后在Manifest文件中使用这个主题。

<application 
      android:allowBackup="true" 
      android:icon="@mipmap/ic_launcher" 
      android:label="@string/app_name" 
      android:theme="@style/MyMaterialTheme"> 
    </application> 

Gradle文件的依赖关系。

dependencies 
     { 
     compile fileTree(dir: 'libs', include: ['*.jar']) compile 

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

'com.android.support:recyclerview-v7:23.1.1' compile 

'com.android.support:cardview-v7:23.2.1' // For NavigationView Using Menu 

'compile 'com.android.support:design:23.3.0' // For Google Map compile 

'com.google.android.gms:play-services-maps:9.8.0' 
     } 

编辑我的主题,只要你想。 希望它会帮助...

+0

请提供您的gradle文件也 –

+0

所以根本不需要使用“android:Theme.Material”?只需使用Theme.AppCompat? – cmingmai

+0

是@CMinMai ...总是我们可以使用,因为我们想.. –

0

试试这个 风格。XML

<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
    </style> 
    <style name="AppTheme.NoActionBar"> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
    </style> 

    <style name="MyRadioButton" parent="Theme.AppCompat.Light"> 
     <item name="colorControlNormal">@color/colorRadioon</item> 
     <item name="colorControlActivated">@color/colorRadioof</item> 
    </style> 

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

</resources> 

style.xml(21节),使用下面的代码

在您添加的gradle aaptOption

<resources>> 
    <style name="AppTheme.NoActionBar"> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
     <item name="android:windowDrawsSystemBarBackgrounds">true</item> 
     <item name="android:statusBarColor">@android:color/transparent</item> 
    </style> 
</resources> 

更新您的摇篮文件

aaptOptions { 
     additionalParameters "--no-version-vectors" 
    } 

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.2" 


    defaultConfig { 
     applicationId "com.jmtechnologies.askuscash" 
     minSdkVersion 16 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 
     multiDexEnabled true 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    dexOptions { 
     javaMaxHeapSize "2g" 
    } 

    // important to run code on kitkat 

    aaptOptions { 
     additionalParameters "--no-version-vectors" 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    // multidex 
    compile 'com.android.support:multidex:1.0.0' 


    compile 'com.android.support:appcompat-v7:25.1.0' 
    compile 'com.android.support:design:25.1.0' 
    compile 'com.android.support:recyclerview-v7:25.1.0' 



} 
2

将以下主题写入style.xml文件。

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary">any_color_code</item> 
    <item name="colorPrimaryDark">any_color_codek</item> 
    <item name="colorAccent">any_color_code</item> 
</style> 

,并写在你的`MainActivity

,并在Manifest.xml文件扩展AppCompatActivity编写应用程序代码如下:

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppBaseTheme"> 
</application> 
+0

所以不需要使用“android:Theme.Material”? – cmingmai

+0

是没有必要使用“android:Theme.Material”,它会自动处理。 –

+0

感谢您的帮助:) – cmingmai

相关问题