android - 将正向按钮和负向按钮设置在警报对话框 fragment 的同一行中

标签 android android-alertdialog android-dialogfragment dialogfragment

对于 API 16,正向按钮和负向按钮一个接一个地排列。在 API 19 及更高版本中,它们属于同一行。更改字体不会更改位置。如何使两个按钮位于同一行?

enter image description here

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.resume_session)
            .setMessage(R.string.session_question)
            .setPositiveButton(R.string.continue_session, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    setDataAndStart(dataHelper, exDB);
                }
            })
            .setNegativeButton(R.string.restart_session, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    exDB.resetCurrentWorkoutRecords();
                    setDataAndStart(dataHelper, exDB);
                }
            });
    final AlertDialog pendingWorkoutAlert = builder.create();
    //check for dp size of the phone and then change font
    if (AppApplication.displayType.equals("normal")) {
        pendingWorkoutAlert.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                Button btnPositive = pendingWorkoutAlert.getButton(Dialog.BUTTON_POSITIVE);
                btnPositive.setTextSize(14);

                Button btnNegative = pendingWorkoutAlert.getButton(Dialog.BUTTON_NEGATIVE);
                btnNegative.setTextSize(14);
            }
        });
    }
    return pendingWorkoutAlert;

最佳答案

问题是您没有指定自己的布局,因此 Android 使用默认布局,因此您无法影响对话框内容的包装方式。

您可以使用AlertDialog.Builder#setView定义您自己的布局。

关于android - 将正向按钮和负向按钮设置在警报对话框 fragment 的同一行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35988194/

相关文章:

android - 窗口关闭后,如何在10秒内打开

java - 如何在 DialogFragment 中设置一分钟间隔

android - 二进制 XML 文件第 7 行 : Duplicate id 0xffffffff, 标记对话框,或带有另一个 fragment 的父 ID 0x0

android - android 中两个带有一个滚动条的 gridView

java - 如何使用adapter.notifyDataSetChanged();我必须使用这些行来解决我的错误

android - 警报对话框背景主题/颜色

android - 在 Android 中扩展 AlertDialog(在哪里可以找到示例,如何获取标题和按钮)

java - Android fragment 显示为 dialogfragment 或通常的 fragment

android - 解析以该对象的最后一个值开头的多个 json 对象值。