如何将 AlertDialog (android.support.v7.app.AlertDialog) 的正面按钮居中只有一个按钮

How to center positive button for AlertDialog (android.support.v7.app.AlertDialog) have only one button

我使用 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>

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

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

这是因为 Rules for aligning buttons

Action buttons are typically Cancel and/or OK, with OK indicating the preferred or most likely action. However, if the options consist of specific actions such as Close or Wait rather than a confirmation or cancellation of the action described in the content, then all the buttons should be active verbs. Order actions following these rules: The dismissive action of a dialog is always on the left. Dismissive actions return to the user to the previous state. The affirmative actions are on the right. Affirmative actions continue progress toward the user goal that triggered the dialog.

您无法更改 positive/negative 按钮的对齐方式。只需将 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>

P.S。我无法检查是否一切正常,因为我的设计 window 在 Studio 中有一些问题,但这是正确的方法并且似乎工作正常。