【问题标题】:My (Specific) layout causes Android Studio XML Layout Preview to keep on loading and not displaying the preview我的(特定)布局导致 Android Studio XML 布局预览继续加载而不显示预览
【发布时间】:2020-05-04 01:46:20
【问题描述】:

我正在制作一个包含 CardView 和嵌套线性布局的布局。当我第一次进行布局时,它工作正常,并且预览窗口也显示了更改。在我打开 android Studio 的一天后,预览窗口一直说“等待构建完成”。所以我认为这是与IDE相关的错误。

我尝试了这里推荐的所有解决方案:

  • 清理项目
  • 重建无效缓存并重新启动
  • 从 Gradle 中删除缓存文件夹

但我发现问题出在 我的布局,因为我删除了布局,重新构建项目并且预览再次正常工作。现在我尝试从头开始再次制作相同的布局,但它仍然再次发生。这对我来说很奇怪,因为我的布局很简单,我之前创建过类似的布局,没有任何问题。 我正在使用最新的 Api 版本 29,以及最新的构建工具和最新的 Android Studio。

这是我的布局代码:

<RelativeLayout 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:background="@color/light"
    tools:context=".Ui.Setting">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/setting_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/light"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ToolbarTheme"
        app:title="SETTINGS"
        app:titleMarginStart="40dp"
        app:titleTextColor="@color/dark" />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/accessibility"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/setting_toolbar"
        android:layout_marginStart="10dp"
        android:padding="10dp"
        android:text="@string/accessibility"
        android:textColor="@color/dark"
        android:textSize="18sp" />

    <androidx.cardview.widget.CardView
        android:id="@+id/accessibility_card"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/accessibility"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        app:cardCornerRadius="8dp"
        app:cardElevation="10dp"
        app:contentPadding="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:baselineAligned="false">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:gravity="center_vertical">

                <com.google.android.material.textview.MaterialTextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10dp"
                    android:layout_weight="1"
                    android:text="@string/grey_scale" />

                <com.google.android.material.switchmaterial.SwitchMaterial
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    app:useMaterialThemeColors="true" />
            </LinearLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@android:color/darker_gray" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center_vertical">

            <com.google.android.material.textview.MaterialTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_weight="0.5"
                android:text="@string/text_size" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:weightSum="3">

                <com.google.android.material.textview.MaterialTextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.5"
                    android:gravity="end"
                    android:text="@string/aminus" />

                <com.google.android.material.slider.Slider
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2" />

                <com.google.android.material.textview.MaterialTextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="10dp"
                    android:layout_weight="0.5"
                    android:gravity="end"
                    android:text="@string/aplus" />

            </LinearLayout>
        </LinearLayout>



    </androidx.cardview.widget.CardView>
    </RelativeLayout>

【问题讨论】:

  • 您是否在 gradle 中添加了材质库以使用 MaterialTextView?
  • 是的,这就是我使用Material TextView的原因,如果没有添加我为什么要使用
  • com.google.android.material.slider.Slider 预览无法在 android studio 上运行,正在等待构建完成显示...是错误吗?

标签: android android-studio android-layout android-cardview


【解决方案1】:

问题的原因是滑块。我也在尝试解决它,但仍然没有成功。

【讨论】:

  • 如果你有任何成功,请告诉我。
【解决方案2】:

抱歉回复晚了...使用AppCompatSlider 或简单地用传统的Seekbar 替换它。材质滑块我也遇到了同样的问题。

【讨论】:

  • Android Studio 4.0 修复了材质滑块问题
猜你喜欢
  • 1970-01-01
  • 2016-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-20
  • 1970-01-01
  • 2022-09-22
  • 1970-01-01
相关资源
最近更新 更多