开发者社区 问答 正文

调用的AlertDialog构建器的按钮工作正常,直到列表需要滚动,然后它们消失

我看到在过去的几年里,有人问过这个问题,但是答案似乎都是命中注定或错失,甚至是偶然的:很少有相同的解决方案,而且没有一种解决方案对我有用。在几乎与我相同的设置中(DialogFragment按钮从屏幕API 24及更高版本中退出),唯一的建议是使用setTitle()而不是setMessage(),但是即使我都不使用它们,滚动时我的按钮仍然消失。但是,如果对话框内容永远不需要滚动,则一切看起来都会正常。

如果我有一个没有子级的ScrollView根,或者将FrameLayout甚至CoordinatorLayout用作子级,这似乎无关紧要。我试图将多汁的layout_marginBottom添加到我的ScrollView中,但这也没有任何改变。

因此,在绝望和沮丧中,我直接吸引了stackoverflow的集体大脑。

这是我的DialogFragment的简化版本:

public class SurfaceDialog extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = requireActivity().getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.surface_dialog, null))
                .setTitle(mMessage)
                .setMultiChoiceItems(mSurfaceList, mCheckedSurfaces, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if (isChecked) {
                            mCheckedSurfacesPositions.add(which);
                        } else {
                            mCheckedSurfacesPositions.remove(mCheckedSurfacesPositions.indexOf(which));
                        }
                    }
                });

        // builder.setCancelable(false);
        builder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ProcessDialogSelections(mCheckedSurfacesPositions);
                dismiss();
            }

        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.onSurfaceDialogSelectedSent(mUnitSurfaces);
                dismiss();
            }
        }).setNeutralButton(mNeutralButtonLabel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dismiss();
            }
        });

        return builder.create();
    }

surface_dialog.XML也是裸露的骨头,因为似乎没有什么改变(我什至可以乘坐CoordinatorLayout而不会影响显示或行为-正面和负面的按钮将正常显示,直到对话框必须滚动为止,然后他们消失了。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:orientation="vertical"
    android:layout_marginBottom="30dp">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</ScrollView>

展开
收起
垚tutu 2019-11-29 12:00:45 715 分享 版权
阿里云 AI 助理回答

你的问题似乎是关于在Android应用中使用DialogFragment时,当对话框内容需要滚动时,底部的按钮(正面、负面和中性按钮)会消失。这通常是因为滚动视图(如ScrollView)占据了所有可用空间,导致底部的按钮被隐藏。为了解决这个问题,你可以尝试以下方法:

方法1:为底部按钮预留空间

在你的surface_dialog.xml布局文件中,可以使用一个包装器来确保底部按钮始终可见。例如,使用LinearLayout作为根布局,并为其设置垂直方向。然后,将大部分内容放在一个权重为1的ScrollView中,以便它填充剩余空间,而底部的按钮直接放置在ScrollView之后,这样它们就不会被滚动影响。

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

    <!-- ScrollView 填充除了底部按钮外的所有空间 -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        
        <!-- 这里是你的滚动内容 -->

    </ScrollView>

    <!-- 底部按钮区域,不会被滚动影响 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="end">

        <!-- 正面、负面和中性按钮放在这里 -->
        <!-- 示例: -->
        <Button
            android:id="@+id/positive_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Done"/>

        <!-- 类似地添加其他按钮 -->

    </LinearLayout>
</LinearLayout>

方法2:使用ConstraintLayout

另一种方法是使用ConstraintLayout,通过约束来精确控制每个元素的位置和大小,确保按钮始终位于屏幕底部。这种方法更加灵活,但可能需要更复杂的约束定义。

注意事项

  • 确保你的DialogFragment没有覆盖或以其他方式修改这些布局属性,导致预期行为改变。
  • 检查是否有任何外部样式或主题可能影响了对话框的展示。
  • 如果你正在使用自定义样式,请确保没有意外地设置了不恰当的padding或margin。

尝试上述方法之一应该能解决你的问题,让底部按钮即使在内容滚动时也能保持可见。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答
问答地址: