带有 RecyclerView 的自定义对话框不显示“确定”取消按钮

我有一个简单的自定义多选项目对话框。如果项目数量很少,则对话框可以正常工作并在底部显示“确定”和“取消”按钮。但如果有很多项目(因此您必须滚动列表)-则不会显示任何按钮。我已经在 SO 中搜索了我的问题,但没有运气。我已经在 Android API 27..29 上测试了我的对话框 - 是一样的。也许我在布局属性等中遗漏了一些重要的东西......

布局/select_exercises_dialog.xml:


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="match_parent"

    android:layout_height="match_parent">



    <androidx.recyclerview.widget.RecyclerView

        android:id="@+id/recyclerView"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

</LinearLayout>

布局/select_exercise_item_view.xml:


<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_width="match_parent"

    android:layout_height="@dimen/list_item_height"

    android:layout_marginLeft="@dimen/margin_medium"

    android:layout_marginRight="@dimen/margin_medium"

    android:gravity="center_vertical">


    <androidx.constraintlayout.widget.ConstraintLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent">


        <TextView

            android:id="@+id/textView"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_margin="@dimen/margin_medium"

            android:text="MyExercise"

            app:layout_constraintBottom_toBottomOf="parent"

            app:layout_constraintStart_toStartOf="parent"

            app:layout_constraintTop_toTopOf="parent" />


    </androidx.constraintlayout.widget.ConstraintLayout>


</FrameLayout>


如何解决此问题并使对话框在所有情况下都显示“确定”、“取消”按钮?任何帮助表示赞赏。


慕桂英546537
浏览 156回答 3
3回答

守着星空守着你

如果您正在使用对话框片段,它是对话框风格的片段,具有一些对话框规范,现在如果您想添加“确定”和“取消”,您有两种选择从 AlertDialog.Builder 扩展类,并在创建时添加正负按钮处理程序和文本关于代码的示例将如下所示class ExampleDialog(context: Context) : AlertDialog.Builder(context) {private val view by lazy {&nbsp; &nbsp; LayoutInflater.from(context).inflate(R.layout.dialog_exmaple, null, false)}override fun setView(layoutResId: Int): AlertDialog.Builder {&nbsp; &nbsp; // do the recylceview init from the view code here&nbsp;&nbsp; &nbsp; return super.setView(view)}override fun setPositiveButton(&nbsp; &nbsp; text: CharSequence?,&nbsp; &nbsp; listener: DialogInterface.OnClickListener?): AlertDialog.Builder {&nbsp; &nbsp; return super.setPositiveButton(&nbsp; &nbsp; &nbsp; &nbsp; context.getText(R.string.action_ok)&nbsp; &nbsp; ) { p0, p1 ->&nbsp; &nbsp; }}override fun setNegativeButton(&nbsp; &nbsp; text: CharSequence?,&nbsp; &nbsp; listener: DialogInterface.OnClickListener?): AlertDialog.Builder {&nbsp; &nbsp; return super.setNegativeButton(&nbsp; &nbsp; &nbsp; &nbsp; context.getText(R.string.action_cancel)&nbsp; &nbsp; ) { p0, p1 ->&nbsp; &nbsp; }}}注意我不喜欢这种分配方式您可以在 xml 内添加两个按钮到回收视图的底部,然后向它们添加文本,例如“确定”和“取消”,并通过高阶函数或界面处理 onClick,这取决于您,我可以向您展示和示例以下我更喜欢这段代码对我来说更干净,并在dialogFragment类中添加点击侦听器<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"><androidx.recyclerview.widget.RecyclerView&nbsp; &nbsp; android:id="@+id/listData"&nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; app:layout_constraintBottom_toTopOf="@id/actionOk"&nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toTopOf="parent" /><com.google.android.material.button.MaterialButton&nbsp; &nbsp; android:id="@+id/actionOk"&nbsp; &nbsp; style="@style/Widget.MaterialComponents.Button.OutlinedButton"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:layout_margin="16dp"&nbsp; &nbsp; android:text="@string/action_ok"&nbsp; &nbsp; app:layout_constraintBottom_toBottomOf="parent"&nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toBottomOf="@id/listData" /><com.google.android.material.button.MaterialButton&nbsp; &nbsp; android:id="@+id/actionCancel"&nbsp; &nbsp; style="@style/Widget.MaterialComponents.Button.OutlinedButton"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:layout_margin="16dp"&nbsp; &nbsp; android:text="@string/action_cancel"&nbsp; &nbsp; app:layout_constraintBottom_toBottomOf="parent"&nbsp; &nbsp; app:layout_constraintEnd_toStartOf="@id/actionOk"&nbsp; &nbsp; app:layout_constraintTop_toBottomOf="@id/listData" />&nbsp; override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {&nbsp; &nbsp; val dialog = super.onCreateDialog(savedInstanceState)&nbsp; &nbsp; val root = ConstraintLayout(activity)&nbsp; &nbsp; root.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)&nbsp; &nbsp; dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)&nbsp; &nbsp; dialog.setContentView(root)&nbsp; &nbsp; dialog.window?.let {&nbsp; &nbsp; &nbsp; &nbsp; it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))&nbsp; &nbsp; &nbsp; &nbsp; it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)&nbsp; &nbsp; &nbsp; &nbsp; it.setWindowAnimations(R.style.DialogAnimationNormal)&nbsp; &nbsp; }&nbsp; &nbsp; dialog.setCanceledOnTouchOutside(true)&nbsp; &nbsp; return dialog}

波斯汪

根据建议的解决方案即兴创作并得出了这个(仍然不完全是我需要的):<?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"    android:id="@+id/linearLayout3"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:minHeight="150dp">    <androidx.recyclerview.widget.RecyclerView        android:id="@+id/recyclerView"        android:layout_width="0dp"        android:layout_height="0dp"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>此布局至少显示“确定”、“取消”按钮和包含项目的列表。但在这种情况下,我的对话框仍然不想占用设备屏幕的所有可用空间。列表recyclerView非常有限(高度只有150dp左右)。当然,我希望对话框占据尽可能多的可用空间。

蝴蝶刀刀

&nbsp; &nbsp;<?xml version="1.0" encoding="utf-8"?>&nbsp; &nbsp; <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; &nbsp; &nbsp; android:orientation="vertical" android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"android:minHeight= "200dp">&nbsp; &nbsp; &nbsp; &nbsp; <androidx.recyclerview.widget.RecyclerView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/recyclerView"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content" />&nbsp; &nbsp; </LinearLayout>&nbsp; &nbsp; <?xml version="1.0" encoding="utf-8"?>&nbsp; &nbsp; <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; &nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginLeft="@dimen/margin_medium"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginRight="@dimen/margin_medium"&nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center_vertical">&nbsp; &nbsp; &nbsp; &nbsp; <androidx.constraintlayout.widget.ConstraintLayout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/textView"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_margin="@dimen/margin_medium"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="MyExercise"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintBottom_toBottomOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintTop_toTopOf="parent" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <CheckBox&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/checkBox"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintBottom_toBottomOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintTop_toTopOf="parent" />&nbsp; &nbsp; &nbsp; &nbsp; </androidx.constraintlayout.widget.ConstraintLayout>&nbsp; &nbsp; </FrameLayout>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java