Android预览无法显示自定义视图,但在设备上可见。

3
我有以下自定义视图:
<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>

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

custom view

然而,如果我将那个自定义视图放入我的活动中:
<?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:

enter image description here

所以,如果我在`MyCustomButtonsView`中添加`tools:layout_height="80dp"`。
<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"/>

它表明按钮本身是问题所在,因为视图变得更大,但按钮仍然不显示:

enter image description here

问题是,如果我在我的设备上运行应用程序,按钮会像预期的那样显示(不需要像 tools 行中设置特定的高度)。
我想知道的是为什么会发生这种情况以及如何避免它。
2个回答

0
看起来这只是Android Studio的一个小故障。在我提问之前,我已经重新构建和重启过了,但没有帮助。一天后,我打开Android Studio,哎呀,按钮显示正常了。所以我猜重启整个系统可能是一个解决办法。

0
对于我的情况,我只是忘记在设计选项卡中更改主题,因为我是Android的新手。
这是之前的样子: enter image description here 之后的样子: enter image description here

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