【问题标题】:Cannot use android:Theme.Material in AppCompatActivity无法在 AppCompatActivity 中使用 android:Theme.Material
【发布时间】:2023-03-14 09:26:01
【问题描述】:

首先,我创建了两种样式,一种是value,一种是value-21。

在 values/styles.xml 中是

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

在 values-v21/styles.xml 中是

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


我的 gradle 文件 (app.gradle)


由于不推荐使用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”?

或者,有没有其他使用材料设计主题的方法?

谢谢。

【问题讨论】:

  • Android L 可以使用 Theme.Meterial 如果使用 AppComatActivity 就使用 Theme.AppCompat
  • 什么情况下会用到“android:Theme.Material”?
  • minSdkVersion 为 21

标签: android android-appcompat android-theme appcompatactivity


【解决方案1】:

在您的 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中写extends AppCompatActivity

并在 Manifest.xml 文件中编写您的应用程序标签,如下所示:

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

【讨论】:

  • 所以根本不需要使用“android:Theme.Material”?
  • 是的,不需要使用“android:Theme.Material”。它将被自动处理。
  • 感谢您的帮助:)
【解决方案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。

最后在 Manifestfile 中使用这个主题。

   <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' 
      } 

根据需要编辑我的主题。 希望它会有所帮助...

【讨论】:

  • 请同时提供您的 gradle 文件
  • 所以根本不需要使用“android:Theme.Material”?只需使用 Theme.AppCompat?
  • 感谢您的帮助:)
  • 是的,总是兄弟......(:
  • 同时使用材料和 appcompat 的好方法
【解决方案3】:

试试这个:

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

【讨论】:

    【解决方案4】:

    试试这个 样式.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(v 21)

    <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>
    

    使用以下代码更新您的 Gradle 文件

    在你的 gradle 中添加 aaptOption

    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'
    
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2016-06-13
      相关资源
      最近更新 更多