Android Studio:渲染问题 缺少样式-为此布局选择了正确的主题,未能找到ID为的样式

18
我想为CardView创建卡片项xml布局,但遇到了这个错误。这里有一些常见的解决方案都无法解决(我已经尝试了它们和其他类似帖子中的解决方案)。
这是我的xml
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="3dp"
        card_view:cardElevation="4dp">


    </android.support.v7.widget.CardView>
</RelativeLayout>

这是 styles.xml 的相关部分:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/primary_accent</item>
</style>

<!--Theme for Tool Bar (Action Bar)-->
<style name="ToolBarTheme" parent="ThemeOverlay.AppCompat.ActionBar">
    <item name="android:textColorPrimary">@color/white</item>
</style>

以下是来自Manifest的相关部分:

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

以下是gradle build脚本的相关部分:

compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.android.support:design:23.0.1'
compile 'com.android.support:cardview-v7:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'

以下是错误信息:

enter image description here

谢谢。


3
尝试在预览区更改 Android API 级别。尝试使用 API 级别 22。 - Akhilesh Kumar
4个回答

39

@viper 请检查你的Android Studio版本和项目中build.gradle文件中的版本是否匹配。不要将其与app的build.gradle混淆。这可能是你遇到的问题。如果它们不匹配,请确保它们匹配,一切应该都会正常。 - MSS

14

您可以通过更换合适的主题来解决此问题。每种主题都不适用于每种视图类型。通过使用主题下拉框,我们可以解决这个问题。
以下是问题 enter image description here

1. 首先进入主题栏并点击它
enter image description here


2. 像下面图片中所示更改您的主题 enter image description here 现在您已经更改了您的主题。如果选定的主题适合您的视图,它将呈现。

enter image description here


4

我也遇到了同样的错误,我的解决方法是将布局主题与manifest.xml中的主题相似,这样就解决了问题。


1

我遇到了类似的问题,我的解决方法是点击刷新按钮。在此输入图片描述

如果问题仍然存在,请尝试无效缓存并重新启动。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接