在警报对话框片段中的同一行中设置正负按钮

时间:2016-03-14 12:57:17

标签: 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;

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

尝试此操作(您可能需要调整宽度以使文本标签适合):

dialog.setOnShowListener(new AlertDialog.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialogInterface) {
        TextView textView = (TextView) dialog.findViewById(android.R.id.message);
        if(textView != null) textView.setTypeface(Utility.getRegularFont(getContext()));

        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setWidth(50);
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setWidth(50);
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setGravity(Gravity.START | Gravity.BOTTOM);
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setGravity(Gravity.END | Gravity.BOTTOM);
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.app_theme_color));
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.app_theme_color));
    }
});

答案 2 :(得分:0)

Create your own custom dialog

public class DeleteSurveyDialog {
    private Dialog mPopDialog;

    private static DeleteSurveyDialog mInstance;

    private DeleteSurveyDialog() {
    }

    /**
     * This method returens the current class object and check object is null or not
     * if null then reinitialize the object otherwise return the exixting
     *
     * @return
     */
    public static DeleteSurveyDialog getInstance() {

        if (mInstance == null) {
            mInstance = new DeleteSurveyDialog();
        }
        return mInstance;
    }

    /*
     * This method display a message or alert for any functionality
     */
    public void displayDeletepopupDialog(final Context mContext, String title, String message) {
        mPopDialog = new Dialog(mContext);
        mPopDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mPopDialog.setContentView(R.layout.survey_delete_popup);
        mPopDialog.getWindow().setBackgroundDrawable(
                new ColorDrawable(android.graphics.Color.TRANSPARENT));
        TextView mTextViewTitle = (TextView) mPopDialog.findViewById(R.id.titleTextView);
        mTextViewTitle.setText(title);
        TextView mTextViewMessage = (TextView) mPopDialog.findViewById(R.id.messageTextView);
        mTextViewMessage.setText(message);
        Button mButtonOK = (Button) mPopDialog.findViewById(R.id.deleteSurveyButton);
        ImageView mButtonClosePopup = (ImageView) mPopDialog.findViewById(R.id.closePopButton);

        mButtonOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {



            }
        });
        mButtonClosePopup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopDialog.dismiss();
            }
        });
        mPopDialog.show();
    }


survey_delete_popup:-

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <android.support.v7.widget.CardView
        android:id="@+id/ly_root"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:background="@color/recyclerdataitemcolor"
        android:elevation="2dp"
        android:translationZ="2dp"
        app:cardCornerRadius="4dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/titleTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:text="@string/deletesurvey"
                android:textColor="@color/black"
                android:textSize="22sp" />

            <View
                android:id="@+id/line1"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@+id/titleTextView"
                android:background="@color/line_color" />

            <TextView
                android:id="@+id/messageTextView"
                android:layout_width="match_parent"
                android:layout_height="120dp"
                android:layout_below="@+id/line1"
                android:gravity="center"
                android:text="@string/deletesurveymessage"
                android:textSize="18sp" />


            <Button
                android:id="@+id/deleteSurveyButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/messageTextView"
                android:layout_centerHorizontal="true"
                android:background="@drawable/dialogbuttonselector"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:text="OK"
                android:textColor="#FFFFFF"
                android:textSize="20sp" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>

    <ImageView
        android:id="@+id/closePopButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"

        android:layout_gravity="right"
        android:elevation="6dp"
        android:src="@mipmap/popup_cross"
        android:translationZ="12dp" />
</FrameLayout>