Android 预览不显示自定义视图,但在设备上可见
Android preview does not show custom view, but it's visible on device
我有以下自定义视图:
<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>
在此布局的预览中,按钮按预期显示。
但是,如果我采用该自定义视图并将其放入我的 activity:
<?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 Studio 中的一个小故障。在问这个问题之前,我已经重建并重新启动了,但没有帮助。一天后,我打开 Android Studio,你瞧,它正确地显示了按钮。所以我想重启整个系统可能是一个解决方案。
我有以下自定义视图:
<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>
在此布局的预览中,按钮按预期显示。
但是,如果我采用该自定义视图并将其放入我的 activity:
<?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 Studio 中的一个小故障。在问这个问题之前,我已经重建并重新启动了,但没有帮助。一天后,我打开 Android Studio,你瞧,它正确地显示了按钮。所以我想重启整个系统可能是一个解决方案。