我想显示带有自定义视图和动作按钮的AlertDialog(自定义视图为ContraintLayout,layout_height = wrap_content
-LinearLayout完美工作)。 ContrainLayout确实占用很小的空间,但ConstraintLayout的父元素全部放在screeen上,因此我看不到操作按钮。这发生在Android设备上,在“设置”->“显示”->“屏幕缩放”中具有最大值。
View contentView =
getLayoutInflater().inflate(R.layout.dialog_with_checkbox, null);
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
.setPositiveButton("Ok", null)
.setMessage(getString(R.string.dialog_309_message))
.setView(contentView)
.create();
alertDialog.show();
R.layout.dialog_with_checkbox
<?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="wrap_content"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="@+id/checkBox"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
为什么ConstraintLayout的父级(这是AlertDialog的内部实现)在屏幕上占据所有位置?
您给出的约束是布局错误。检查下面的布局对我来说是完美的工作。
<?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="wrap_content"
android:padding="@dimen/_15sdp">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="@dimen/_50sdp"
android:text="Hello World! asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf as "
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Never show this message again."
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
这里输出