【问题标题】:AlertDialog with custom view(with ConstraintLayout) takes all screen space(wrap_content of ConstraintLayout isn't work)带有自定义视图的 AlertDialog(带有 ConstraintLayout)占用所有屏幕空间(ConstraintLayout 的 wrap_content 不起作用)
【发布时间】:2020-01-28 14:23:16
【问题描述】:

我想显示带有自定义视图和操作按钮的 AlertDialog(自定义视图是具有 layout_height=wrap_content 的 ContraintLayout - LinearLayout 完美)。 ContrainLayout 确实需要很小的尺寸,但 ConstraintLayout 的父级全部出现在屏幕上,因此我看不到操作按钮。这发生在 Android 设备上,其中 Settings -> Display -> Screen Zoom 具有最大值。

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>

为什么约束布局的父级(这是 AlertDialog 的内部实现)都在屏幕上?

【问题讨论】:

  • 你检查我的答案了吗?

标签: android android-alertdialog android-constraintlayout


【解决方案1】:

您给出的约束是布局错误。检查下面的布局,这对我来说是完美的工作。

<?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>

这是输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 2017-04-12
    • 2019-03-29
    • 2019-05-01
    • 2022-01-11
    • 1970-01-01
    • 2012-05-26
    相关资源
    最近更新 更多