【问题标题】:How to center positive button for AlertDialog (android.support.v7.app.AlertDialog) have only one button如何将 AlertDialog 的正按钮居中(android.support.v7.app.AlertDialog)只有一个按钮
【发布时间】:2017-06-29 00:48:29
【问题描述】:

我使用 AlertDialog (android.support.v7.app.AlertDialog) 来显示游戏级别,如下所示

AlertDialog dialog;
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.DialogLevelsStyle);
                LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View view = inflater.inflate(R.layout.dialog_game_level, null);
    builder.setView(view)
                        .setCancelable(false)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (dialog != null) {
                                    dialog.cancel();
                                }
                            }
                        });

                dialog = builder.create();
                dialog.show();

//styles.xml

<style name="DialogLevelsStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">#3caaf0</item>
</style>

但对话框在警告对话框的按钮右侧显示肯定按钮确定,请帮助我!

我通过添加图片更新问题:

【问题讨论】:

  • 试试 setNeutralButton()。
  • 使用 setNeutralButton() 显示在 AlertDialog 的左下角

标签: android android-appcompat


【解决方案1】:

这是因为Rules for aligning buttons

操作按钮通常是取消和/或确定,确定表示首选或最可能的操作。但是,如果选项由诸如关闭或等待之类的特定操作组成,而不是内容中描述的操作的确认或取消,那么所有按钮都应该是活动动词。遵循以下规则的命令操作: 对话的不屑一顾的动作总是在左边。解除操作将用户返回到之前的状态。 平权行动在右边。肯定的行动继续朝着触发对话的用户目标前进。

您无法更改正/负按钮的对齐方式。只需将Button 添加到您的view 中,然后将dialog 与中心对齐并按如下方式进行处理:

Button btn = (Button) view.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
         if (dialog != null) {
             dialog.cancel();
         }
    }
});

更新 它应该是这样的:

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">
            <TextView
                 android:text="something"
                 android:id="@+id/id1"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content" />
    </ScrollView>

    <Button
        android:id="@+id/id2"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
</RelativeLayout>

附:我无法检查一切是否正常,因为我的设计窗口在 Studio 中存在一些问题,但这是正确的方法,并且似乎工作正常。

【讨论】:

  • 问题是我的警报对话框有很长的内容(必须使用 ScrollView 滚动才能看到我的内容)所以如果我在视图末尾添加了一个自定义按钮。之后使用 builder.setView(view) 设置自定义视图。我必须滚动到结束才能看到确认按钮。我也可以使用相对布局将按钮挂在布局底部,但它显示得很奇怪。而且我不想用这种方式接近
  • 不,您将滚动视图设置在与按钮相同的级别,因此您将在滚动视图中看到按钮和滚动。我将在几分钟内通过示例更新答案
  • 只是关于您的设计窗口的旁注,尝试将 API 级别设置为 21 而不是 22,它为我解决了问题。
  • @Marcus,在工作中我在这个项目中仍然有 20.0.0,在另一台笔记本电脑上 22.0.0 没有什么可抱怨的。但是谢谢你,虽然很好:)
  • @Yurets:使用该方法,对话框将始终显示全尺寸,尽管内容不够长
猜你喜欢
  • 2012-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-17
  • 2021-07-20
  • 1970-01-01
  • 1970-01-01
  • 2017-01-21
相关资源
最近更新 更多