Android предварительный просмотр не показывает пользовательский вид, но он виден на устройстве - PullRequest
0 голосов
/ 08 января 2020

У меня есть следующий пользовательский вид:

<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

Итак, если я добавлю 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"/>

Это показывает, что сами кнопки являются проблемой, потому что представление увеличивается, но кнопки по-прежнему не отображаются:

enter image description here

Дело в том, что, если я запускаю приложение на своем устройстве, кнопки показывать как положено (без указания указанного c высоты как в строке tools).

Я хочу знать, почему это происходит и как этого избежать.

1 Ответ

0 голосов
/ 12 января 2020

Похоже, это был просто сбой в Android Studio. Я восстановил и перезапустил, прежде чем я задал этот вопрос, и это не помогло. Через день я открываю Android Studio и, о чудо, он показывает кнопки правильно. Поэтому я думаю, что перезапуск всей системы может быть решением.

...