如何在自定义对话框中放置多个线性布局?

How to place multiple linearlayout in custom dialogs?

我创建了一个带有在运行时填充的单选按钮的自定义对话框。如果单选按钮的数量超过 12,则底部线性布局(带按钮)不显示。我该如何解决这个问题?我的代码


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

 <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    android:orientation = "vertical"
    android:padding = "16dp">

<LinearLayout
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content"
    android:layout_marginBottom = "100dp">

    <ScrollView
        android:layout_width = "match_parent"
        android:layout_height = "match_parent">

        <RadioGroup
            android:id = "@+id/input_radio_button_radiogroup"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"></RadioGroup>
    </ScrollView>
</LinearLayout>

<LinearLayout
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content"
    android:orientation = "horizontal">

    <com.google.android.material.button.MaterialButton
        android:id = "@+id/input_radio_button_custom_cancel_button"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:layout_marginRight = "4dp"
        android:layout_weight = "1"
        android:text = "Cancel" />

    <com.google.android.material.button.MaterialButton
        android:id = "@+id/input_radio_button_custom_ok_button"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:layout_marginLeft = "4dp"
        android:layout_weight = "1"
        android:text = "Ok" />
</LinearLayout>
</LinearLayout>

包裹 ScrollView 的 LinearLayout 的高度设置为“wrap_content”,当列表越大时,它会将按钮推出屏幕。

我建议使用 ConstraintLayout,它允许您相对于彼此定位视图。例如“按钮总是在父级的底部对齐,ScrollView 总是在按钮上方和父级顶部下方”

只需将您的父级 LinearLayout 替换为 FrameLayout 并将 Bottom 容器 (LinearLayout) 重力设置为 Bottom

<FrameLayout xmlns:android = "http://schemas.android.com/apk/res/android"
     android:layout_width = "match_parent"
     android:layout_height = "match_parent"
     android:padding = "16dp">

<LinearLayout
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content"
    android:layout_marginBottom = "100dp">

    <ScrollView
        android:layout_width = "match_parent"
        android:layout_height = "match_parent">

        <RadioGroup
            android:id = "@+id/input_radio_button_radiogroup"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"></RadioGroup>
    </ScrollView>
</LinearLayout>

<LinearLayout
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content"
    android:layout_gravity="bottom"
    android:orientation = "horizontal">

    <Button
        android:id = "@+id/input_radio_button_custom_cancel_button"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:layout_marginRight = "4dp"
        android:layout_weight = "1"
        android:text = "Cancel" />

    <Button
        android:id = "@+id/input_radio_button_custom_ok_button"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:layout_marginLeft = "4dp"
        android:layout_weight = "1"
        android:text = "Ok" />
</LinearLayout>