首页 > 解决方案 > 带有 RecyclerView 的自定义对话框在屏幕上膨胀得非常小

问题描述

我创建了一个对话框函数,它在屏幕上创建一个对话框,您可以在其中从 RecyclerView 中选择一些内容(底部的 RView 适配器代码):

fun showSelectACategoryDialog(context: Context,allCategories: LiveData<List<NotesCategory>>, owner: Fragment,chosenCategoryID: (categoryID: Long) -> Unit) {

    val dialog = Dialog(context)

    val adapter = DialogCategoriesRecyclerViewAdapter(context, chosenCategoryID)

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.setCancelable(true)
    dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    dialog.setContentView(R.layout.dialog_select_category)

    dialog.dialog_select_a_category_recyclerView.adapter = adapter

    allCategories.observe(owner, Observer {
        adapter.submitList(it)
    })

// Click listeners

   dialog.show()

然后使用以下命令调用对话框函数:

 binding.notesCategoryTextView.setOnClickListener {
            showSelectACategoryDialog(
                context!!,
                notesEditViewModel.allCategories,
                NotesEditFragment(),
                selectedCategory(notesCategory)
            )
        }

但是当对话框被调用时,它看起来像这样: 真正的小型对话框出现

这是对话框的回收器视图适配器的代码,以防相关:


class DialogCategoriesRecyclerViewAdapter(
    var context: Context,
    var chosenCategoryID: (categoryID: Long) -> Unit
) : ListAdapter<NotesCategory, DialogCategoriesRecyclerViewAdapter.ViewHolder>(SingleCategoryDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder = ViewHolder.from(parent)

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        holder.bind(getItem(position), context)

        holder.itemView.setOnClickListener {
            chosenCategoryID(getItem(position).categoryID)
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val categoryNameTextView: TextView = itemView.findViewById(R.id.dialog_select_a_category_textView)

        fun bind(item: NotesCategory, context: Context) {
            categoryNameTextView.text = item.categoryName
        }

        companion object {
            fun from(parent: ViewGroup): ViewHolder {
                val view = LayoutInflater.from(parent.context).inflate(
                    R.layout.row_dialog_select_category_all_categories, parent, false
                )
                return ViewHolder(view)
            }
        }
    }
}

这是不起作用的对话框的 XML 布局:

<?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:layout_margin = "16dp"
    android:background = "@drawable/dialog_rounded_background">

    <TextView
        android:id = "@+id/dialog_select_a_category_title"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_marginTop = "16dp"
        android:text = "@string/dialog_categories_title"
        android:textColor = "@color/Black"
        android:textSize = "25sp"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintHorizontal_bias = "0.5"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toTopOf = "parent" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id = "@+id/create_new_category_button"
        android:layout_width = "0dp"
        android:layout_height = "wrap_content"
        android:layout_marginStart = "16dp"
        android:layout_marginTop = "16dp"
        android:layout_marginEnd = "16dp"
        android:background = "@drawable/border_square_transparent"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintHorizontal_bias = "0.5"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toBottomOf = "@+id/dialog_select_a_category_title"
        tools:ignore = "RtlSymmetry">

        <ImageView
            android:id = "@+id/_imageview"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:layout_marginStart = "16dp"
            app:layout_constraintBottom_toBottomOf = "parent"
            app:layout_constraintStart_toStartOf = "parent"
            app:layout_constraintTop_toTopOf = "parent"
            app:srcCompat = "@drawable/dialog_plus"
            tools:ignore = "ContentDescription" />

        <TextView
            android:id = "@+id/_textview"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:layout_margin = "16dp"
            android:layout_marginStart = "56dp"
            android:text = "@string/dialog_create_new_category_button"
            android:textColor = "@color/Black"
            android:textSize = "20sp"
            app:layout_constraintBottom_toBottomOf = "parent"
            app:layout_constraintEnd_toEndOf = "parent"
            app:layout_constraintStart_toEndOf = "@+id/_imageview"
            app:layout_constraintTop_toTopOf = "parent"
            app:layout_constraintVertical_bias = "0.506" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id = "@+id/dialog_select_a_category_recyclerView"
        android:layout_width = "0dp"
        android:layout_height = "0dp"
        android:layout_marginStart = "16dp"
        android:layout_marginTop = "16dp"
        android:layout_marginEnd = "16dp"
        android:layout_marginBottom = "20dp"
        android:background = "@drawable/border_square_transparent"
        app:layoutManager = "androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf = "parent"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintHorizontal_bias = "0.5"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toBottomOf = "@+id/create_new_category_button"
        tools:listitem = "@layout/row_dialog_select_category_all_categories" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是有效的对话框的 XML 布局

<?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:layout_margin = "16dp"
    android:background = "@drawable/dialog_rounded_background">

    <ImageButton
        android:id = "@+id/dialog_create_category_confirm"
        android:layout_width = "50dp"
        android:layout_height = "50dp"
        android:layout_marginStart = "208dp"
        android:layout_marginTop = "64dp"
        android:layout_marginEnd = "16dp"
        android:layout_marginBottom = "16dp"
        android:background = "@android:color/transparent"
        android:src = "@drawable/dialog_check"
        app:layout_constraintBottom_toBottomOf = "parent"
        app:layout_constraintEnd_toEndOf = "@+id/dialog_create_category_category_name"
        app:layout_constraintStart_toEndOf = "@+id/dialog_create_category_cancel"
        app:layout_constraintTop_toBottomOf = "@+id/dialog_create_category_category_name"
        tools:ignore = "ContentDescription" />

    <ImageButton
        android:id = "@+id/dialog_create_category_cancel"
        android:layout_width = "50dp"
        android:layout_height = "50dp"
        android:layout_marginStart = "16dp"
        android:layout_marginTop = "64dp"
        android:layout_marginBottom = "16dp"
        android:background = "@android:color/transparent"
        android:src = "@drawable/dialog_cancel"
        app:layout_constraintBottom_toBottomOf = "parent"
        app:layout_constraintStart_toStartOf = "@+id/dialog_create_category_category_name"
        app:layout_constraintTop_toBottomOf = "@+id/dialog_create_category_category_name"
        tools:ignore = "ContentDescription" />

    <EditText
        android:id = "@+id/dialog_create_category_category_name"
        android:layout_width = "0dp"
        android:layout_height = "wrap_content"
        android:layout_marginStart = "16dp"
        android:layout_marginTop = "32dp"
        android:layout_marginEnd = "16dp"
        android:ems = "10"
        android:hint = "@string/dialog_create_category_hint"
        android:importantForAutofill = "no"
        android:inputType = "textPersonName|textCapSentences"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintHorizontal_bias = "0.487"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toBottomOf = "@+id/dialog_create_category_title" />

    <TextView
        android:id = "@+id/dialog_create_category_title"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_marginTop = "16dp"
        android:text = "@string/dialog_create_a_category_title"
        android:textColor = "@color/Black"
        android:textSize = "30sp"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintHorizontal_bias = "0.5"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toTopOf = "parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

通过单击“@+id/create_new_category_button”(这是屏幕上唯一显示的内容)调用第二个对话框,它可以很好地填充屏幕宽度减去边距

标签: androidkotlinandroid-recyclerviewdialog

解决方案


您必须为对话框的布局定义固定大小,或者只创建一个全宽对话框。

尝试这个:

dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

推荐阅读