Android Studio 预览未显示布局

3

我刚开始学习Android Studio,遇到了一个问题:

enter image description here

在我添加布局后,预览中不显示它们。但是,如果我更改主题或在模拟器中运行应用程序,则它们会出现,但会重叠。


1
渲染问题是什么?请给我看Gradle文件! - Abner Escócio
显示“加载Apcompat actionBar失败,原因未知” - Errahbi Zouhair
请使用正确的API级别来处理您的布局组件。也许将其更改为26或更低版本,并调整依赖项,它就可以正常工作了。请尝试一下! - Abner Escócio
我无法更改API级别..唯一可用的级别是28 :/ - Errahbi Zouhair
兄弟,不幸的是这个问题是由此原因引起的。我使用了26或更低版本来解决这个问题。当使用28及其依赖项指向28.0.0-alpha3时,我的CoordinatorLayout工作得不好。 - Abner Escócio
@AbnerEscócio 请在API 28中使用我答案中的解决方案。 - MRah
1个回答

7

对于API 27:

添加以下依赖项,如果您正在使用targetSdkVersion 27,在应用程序级别gradle文件build.gradle(Module: app)中:

implementation 'com.android.support:appcompat-v7:27.1.1'

对于API 28:

如果您正在使用targetSdkVersion 28,则需要执行以下步骤:

步骤1:确保您具有以下依赖项:

implementation 'com.android.support:appcompat-v7:28.0.0-rc02'

更新:有关最新的appcompat-v7库版本,请访问此链接:

https://mvnrepository.com/artifact/com.android.support/appcompat-v7?repo=google

步骤2:

进入“Values”>“styles.xml”,并将样式修改为以下内容:

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">

你可以选择任何你喜欢的基础主题。

这里是一个约束布局的示例:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <EditText
        android:id="@+id/enter_age"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:ems="10"
        android:hint="enter age"
        android:inputType="number"
        app:layout_constraintStart_toStartOf="parent" 
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/button1"
        app:layout_constraintVertical_chainStyle="packed" />


    <Button
        android:id="@+id/button1"
        android:layout_width="200dp"
        android:layout_height="110dp"
        android:onClick="butGetAge"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/enter_age"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

非常抱歉问这个问题 xD。但我是Android Studio的新手,你说的是什么意思:在您的应用程序级别gradle文件build.gradle(Module:app)中添加以下依赖项:implementation 'com.android.support:appcompat-v7:27.1.1' - Errahbi Zouhair
更新:我已经检查了Gradle文件,发现已经有一个实现'com.android.support:appcompat-v7:28.0.0-alpha3'。 - Errahbi Zouhair
@ErrahbiZouhair 请在你的 build.gradle 中使用 API 27。将 compileSdkVersion 和 targetSdkVersion 改为 27,并使用 'com.android.support:appcompat-v7:27.1.1'。 - MRah
2
@ErrahbiZouhair 请检查更新后的答案。如果您想使用targetSdkVersion 28,则转到values>styles.xml并将父项修改为parent="Base.Theme.AppCompat.Light.DarkActionBar"。 - MRah
2
是的,我也遇到了同样的问题。parent="Base.Theme.AppCompat.Light.DarkActionBar"解决了它。 - SynAck
是的,最后一个解决方案就是它!!非常感谢你! - Errahbi Zouhair

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