【问题标题】:Android preview does not show custom view, but it's visible on deviceAndroid 预览不显示自定义视图,但在设备上可见
【发布时间】:2020-01-08 13:55:31
【问题描述】:

我有以下自定义视图:

<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/declineButton"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/confirmButton"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:text="decline"/>

    <Button
        android:id="@+id/confirmButton"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/declineButton"
        app:layout_constraintBottom_toBottomOf="@id/declineButton"
        app:layout_constraintStart_toEndOf="@id/declineButton"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="0dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:text="confirm"/>

</androidx.constraintlayout.widget.ConstraintLayout>

在此布局的预览中,按钮按预期显示。

但是,如果我采用该自定义视图并将其放入我的活动中:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/backgroundColor">

    <TextView
        android:id="@+id/textAbove"
        style="@style/someStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/something"
        app:layout_constraintBottom_toTopOf="@id/buttons"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:text="text above"/>

    <MyCustomButtonsView
        android:id="@+id/buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@id/textBelow"
        app:layout_constraintTop_toBottomOf="@id/textAbove"/>

    <TextView
        android:id="@+id/textBelow"
        style="@style/someStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginBottom="16dp"
        android:text="text below"/>
</androidx.constraintlayout.widget.ConstraintLayout>

按钮不显示在预览中,因为自定义视图的高度为 0:

所以,如果我将tools:layout_height="80dp" 添加到MyCustomButtonsView

<MyCustomButtonsView
        android:id="@+id/buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout_height="80dp
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@id/textBelow"
        app:layout_constraintTop_toBottomOf="@id/textAbove"/>

这表明按钮本身是问题,因为视图变大了,但按钮仍然不显示:

问题是,如果我在我的设备上运行应用程序,按钮会按预期显示(没有像 tools 行中那样设置特定的高度)。

我想知道的是为什么会发生这种情况以及如何避免它。

【问题讨论】:

    标签: android android-studio android-layout preview


    【解决方案1】:

    看起来这只是 Android Studio 中的一个小故障。在我问这个问题之前,我已经重建并重新启动它并没有帮助。一天后,我打开 Android Studio,你瞧,它正确地显示了按钮。所以我想重新启动整个系统可能是一个解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-18
      • 2018-08-26
      • 1970-01-01
      • 2017-06-29
      • 2017-03-18
      • 1970-01-01
      • 2023-03-04
      • 2017-06-30
      相关资源
      最近更新 更多