【问题标题】:Android Studio 3.0 Layout Editor/PreviewerAndroid Studio 3.0 布局编辑器/预览器
【发布时间】:2018-05-21 07:15:05
【问题描述】:

我正在使用 Android Studio 3.0 制作一个 Android 应用程序。

但我面临的问题是布局编辑器/预览器无法正常工作。它有时不显示任何内容,有时显示一个带有所有导航按钮的灰色框。

这些是截图

我使用的是buildToolsVersion '26.0.2',支持库版本是23.0.1

任何帮助都会非常有帮助。

编辑:这是我的 XML 布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"/>

        <ImageView
            android:id="@+id/logo"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_gravity="center"
            android:background="@drawable/logo" />

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#FFFFFF"
            android:textSize="33dp"
            android:textStyle="bold" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="15dp">
            <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:textColor="#FFFFFF" />

     </LinearLayout>

     </LinearLayout>

</RelativeLayout>

【问题讨论】:

  • 如果项目尚未构建,预览可能为空或错误。但如果已经构建,那么快速修复就是重新构建或清理项目......
  • ^ 试试上面的方法,如果还是不行就去File &gt; invalidate cache/restart
  • @Xenolion 感谢您的回答。我尝试过重建、清理、使缓存无效和重新启动,但没有一个解决方案真正起作用。有什么想法吗?
  • @MohammedAlSafwan 也尝试过,但没有奏效。我可以尝试任何其他解决方案吗?
  • 你的布局有视图吗???

标签: java android android-layout layout android-gradle-plugin


【解决方案1】:

您在构建 UI 时遇到了一些问题。这是一个修复,还要确保@drawable/logo 文件位于drawable folder

   <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/logo"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:orientation="horizontal" />

<ImageView
    android:id="@+id/logo"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_marginBottom="10dp"
    android:background="@drawable/logo"
    app:layout_constraintBottom_toTopOf="@+id/name"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

<TextView
    android:id="@+id/name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:background="#FFF"
    android:textColor="#000"
    android:textSize="33sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/logo" />

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="207dp"
    android:background="#FFF"
    android:textColor="#000"
    android:textSize="33sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/name" />

【讨论】:

  • 再次感谢。只是为了了解我编写的布局中的错误是什么,我自己仍然无法识别它们。并且是否必须使用约束布局?
  • 他们不是错误,而是更多的逻辑结构问题。你有一个 textview 有 wrap_content 因为它是宽度,但没有内容,所以它没有出现。你也有没有理由存在的嵌套视图,所以我也为你解决了这个问题。
  • 谢谢。如果我将来有任何问题会与您联系:)。
猜你喜欢
  • 2014-09-10
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-15
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多