代码之家  ›  专栏  ›  技术社区  ›  Cruces

扩展AlertDialog在对话框底部创建大填充

  •  0
  • Cruces  · 技术社区  · 7 年前

    我正在尝试创建一个使用自定义视图的对话框,该视图还附加了许多不同的行为,这就是为什么我扩展了AlertDialog而不是使用生成器的原因。

    创建视图的代码如下所示

    class MyTaskCreateDialog extends AlertDialog implements DialogInterface.OnCancelListener {
    
        public MyTaskCreateDialog(Context ctx, TaskItem item, List<Person> people, IOnFinishListener listener) {
            super(ctx) {
            View view = LayoutInflater.from(ctx).inflate(Resource.Layout.dialog_my_task_create, null, false);
            setView(view);
            setCancelable(true);
            setOnCancelListener(this);
            setCanceledOnTouchOutside(true);
            assign_views(view); //this assigns the views to the local variables
            attach_listeners(); 
            refresh_view();//this updates the view with data
        }
    

    上面使用这个布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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/app_padding">
    
        <android.support.design.widget.TextInputLayout
            android:id="@+id/my_task_create_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/app_margin"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <android.support.design.widget.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/my_task_create_hint_title" />
        </android.support.design.widget.TextInputLayout>
    
        <android.support.design.widget.TextInputLayout
            android:id="@+id/my_task_create_description"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="@+id/my_task_create_title"
            app:layout_constraintStart_toStartOf="@+id/my_task_create_title"
            app:layout_constraintTop_toBottomOf="@+id/my_task_create_title">
    
            <android.support.design.widget.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/my_task_create_hint_description" />
        </android.support.design.widget.TextInputLayout>
    
        <TextView
            android:id="@+id/my_task_create_assignee_caption"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/my_task_create_caption_assignee"
            app:layout_constraintBottom_toBottomOf="@+id/my_task_create_assignee"
            app:layout_constraintStart_toStartOf="@+id/my_task_create_description"
            app:layout_constraintTop_toTopOf="@+id/my_task_create_assignee" />
    
        <Spinner
            android:id="@+id/my_task_create_assignee"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/app_margin"
            android:checked="true"
            android:padding="@dimen/app_padding"
            android:textOff="@string/my_task_create_toggle_manually"
            android:textOn="@string/my_task_create_toggle_automatically"
            app:layout_constraintEnd_toEndOf="@+id/my_task_create_description"
            app:layout_constraintStart_toEndOf="@+id/my_task_create_assignee_caption"
            app:layout_constraintTop_toBottomOf="@+id/my_task_create_description"
            tools:listitem="@android:layout/simple_spinner_item" />
    
    
        <Button
            android:id="@+id/my_task_create_create_task"
            style="@style/Widget.AppCompat.Button.Borderless.Colored"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/app_margin"
            android:text="@string/my_task_create_button_create"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/my_task_create_cancel"
            app:layout_constraintTop_toBottomOf="@+id/my_task_create_assignee" />
    
        <Button
            android:id="@+id/my_task_create_cancel"
            style="@style/Widget.AppCompat.Button.Borderless.Colored"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/app_margin"
            android:text="@string/my_task_create_button_cancel"
            app:layout_constraintEnd_toStartOf="@+id/my_task_create_create_task"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/my_task_create_assignee" />
    
    </android.support.constraint.ConstraintLayout>
    

    它会创建一个底部有大填充的对话框。

    实际上,我将约束布局的背景更改为灰色,那么对话框的下半部分似乎是默认颜色。

    以上结果如下:

    the dialog that is shown

    现在我知道为按钮保存了一些空间(我不使用这些按钮,而是使用布局文件中的自定义按钮),但是如何使其余的空间消失并使我的对话框包装其内容呢?

    1 回复  |  直到 7 年前
        1
  •  0
  •   0xCursor    6 年前

    我也遇到了同样的问题,唯一有效的解决方案是将以下代码添加到所有代码中 View S在 ConstraintLayout :

    android:layout_height="0dp"
    app:layout_constraintHeight_default="wrap"
    

    这些句子取代了这个:

    android:layout_height="wrap_content"