【问题标题】:Set Done Button on Kotlin DialogFragment with multi choice Items在 Kotlin DialogFragment 上使用多项选择项设置完成按钮
【发布时间】:2020-01-13 01:21:23
【问题描述】:

我有一个多选样式的 DialogFragment,这意味着它不会在选择/取消选择时关闭。消除它的唯一方法似乎是单击设备的“后退”按钮。

我发现单击设备后退按钮关闭对话框很奇怪。 有没有办法在对话框中添加一个Done 按钮,在标题旁边?

【问题讨论】:

  • 您使用的是 AlertDialog 还是什么?
  • @MohammedAlaa 是的,我是先生。
  • 我目前正在尝试这个:stackoverflow.com/questions/25530127/…
  • 有一个答案指出您应该调整按钮的布局-婴儿车,您也可以尝试其他解决方案,例如link1link2
  • @MohammedAlaa 这些链接只是展示了如何创建多个选择的对话框。我的问题是在对话框主标题(顶部)旁边添加一个按钮。如这里:stackoverflow.com/questions/25530127/…

标签: android kotlin dialog fragment


【解决方案1】:

其实很简单,我正在扩展theoretical answer here

创建您的自定义布局,例如 myLayoutTitle.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/select_type_title_container"
    android:background="#f6f6f6">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        app:layout_constraintLeft_toLeftOf="@+id/select_type_title_container"
        app:layout_constraintTop_toTopOf="@+id/select_type_title_container"
        android:text="@string/menu_types"
        android:layout_gravity ="center_vertical"
        android:gravity="center_vertical"
        android:textStyle="bold"
        android:textSize="20dp"
        android:id="@+id/select_types_textview"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        app:layout_constraintRight_toRightOf="@+id/select_type_title_container"
        app:layout_constraintTop_toTopOf="@+id/select_type_title_container"
        android:layout_margin="5dp"
        android:text="Done"
        android:textColor="@color/colorPrimary"
        android:id="@+id/done_select_types"
        android:layout_gravity ="center_vertical"/>
</androidx.constraintlayout.widget.ConstraintLayout>

对话框生成器上的

builder.setCustomTitle(activity?.layoutInflater?.inflate(R.layout.myLayoutTitle, null))

【讨论】:

    猜你喜欢
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    相关资源
    最近更新 更多