Android Studio 中的渲染问题 [英] Rendering Issue in Android Studio
问题描述
我是 Android Studio 的新手.我在 activity_main.xml 选项卡下遇到了这个问题.那么,如何解决这个问题.我已经离线安装了 android studio 1.4.1 包.此外,我无法在activity_main.xml 中将小部件拖放到电话中.但我可以在content_main.xml 中拖放
I am new to Android Studio.And I came accross this problem under activity_main.xml tab . So, How to fix this.I have installed the android studio 1.4.1 bundle offline. Also I can't drag and drop widgets to phone in activity_main.xml.But I can drag and drop in content_main.xml
Rendering Problems
The following classes could not be instantiated:
- android.support.design.widget.CoordinatorLayout (Open Class, Show Exception, Clear Cache)
- android.support.design.widget.AppBarLayout (Open Class, Show Exception, Clear Cache)
Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE
Exception Details
java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:34)
at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:178)
at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:172)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
Copy stack to clipboard
这是我的activity_main.xml 文本
Here is my activity_main.xml in text
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
推荐答案
由于你的 MainActivity 扩展了 AppCompatActivity,它的主题大部分来自 AppCompat.要解决此问题,请更正以下内容:
Since your MainActivity extends AppCompatActivity, its theme most be from AppCompat. To fix this issue, correct the following:
- 打开文件app/res/values/styles.xml
- 将 styles.xml 文件中的以下几行更改为如下所示:
- Open the file app/res/values/styles.xml
- Change the following lines in the styles.xml file to look like this:
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
和
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
.
对于您选择的布局,它们都需要从 AppCompat 继承.请参阅下面的默认工作 styles.xml 文件.您可以将其复制并粘贴到您的 styles.xml 文件中.
They all need to inherit from AppCompat for your chosen layout. See the default working styles.xml file below. You can copy and paste it into your styles.xml file.
<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="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
- 如果仍有问题,请转到 app/manifests/AndroidManifest.xml 文件并确保主题的值如下所示
android:theme="@style/AppTheme.NoActionBar"
或这个android:theme="@style/AppTheme"
这样他们就继承了上面列出的 AppCompat 主题.
- If you are still having issues, go to the app/manifests/AndroidManifest.xml file and make sure that the value for the theme looks like this
android:theme="@style/AppTheme.NoActionBar"
or thisandroid:theme="@style/AppTheme"
this way they are inheriting from the AppCompat themes as listed above.
参见此处:您需要在此活动中使用 Theme.AppCompat 主题(或后代)
这篇关于Android Studio 中的渲染问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!