在“警报”对话框中平均分配“确定”和“取消”按钮

时间:2015-10-02 05:14:20

标签: android android-alertdialog

我有警告对话框按钮的这个问题 我已经使用此

成功地在数据库中添加了一些数据

enter image description here

但我的目标是这样的 enter image description here

任何帮助将不胜感激:) 这是我的代码

mylistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        LayoutInflater layoutInflater = LayoutInflater.from(context);
                        View promptView = layoutInflater.inflate(R.layout.activity_prompt_attendance, null);
                        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
                        alertDialogBuilder.setView(promptView);
                        alertDialogBuilder.setTitle("Attendance:");

                        alertDialogBuilder
                                .setCancelable(false)
                                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {


                                    }

                                })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();

                            }
                        });
                        AlertDialog alertD = alertDialogBuilder.create();
                        alertD.show();

                    }
                });

这是thepromptAttendance xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:gravity="center_vertical"
tools:context="com.ucu.ccs.classrecord.promptAttendance">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="20dp">
    <EditText
        android:id="@+id/Date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:hint="Date"/>
    <RadioGroup
        android:id="@+id/radio1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">
        <RadioButton
            android:id="@+id/present"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Present"/>
        <RadioButton
            android:id="@+id/absent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Absent"/>
    </RadioGroup>

</LinearLayout>

这是promptAttendance java文件

public void goAdd(View view){
    new AttemptGetData().execute();
}

class AttemptGetData extends AsyncTask<String, String, String>{
    String date = Date.getText().toString();
    String remarks = RadioAttBtn.getText().toString();

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(promptAttendance.this);
        pDialog.setMessage("In Progress...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        List<NameValuePair> mList = new ArrayList<NameValuePair>();
        mList.add(new BasicNameValuePair("date", date));
        mList.add(new BasicNameValuePair("remark", remarks));

        Log.d("starting", "fetch");

        JSONObject json = jsonParser.makeHttpRequest(url, "POST", mList);

        try {
            verify = json.getString("Message");
            return verify;
        }catch (JSONException e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        pDialog.dismiss();
        if (s != null){
            Toast.makeText(getApplicationContext(), verify, Toast.LENGTH_LONG).show();
            Date.setText(null);
        }
    }
}

3 个答案:

答案 0 :(得分:1)

重量不适合我(我的测试手机是galaxy s3 5.0.1)
enter image description here

所以我稍微改变一下。

  alert.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            Button posButton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
            Button negButton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
            View parent =(View)posButton.getParent();
            parent.setPadding(0,0,0,0);

            LinearLayout.LayoutParams posParams = (LinearLayout.LayoutParams) posButton.getLayoutParams();
            posParams.width = parent.getWidth()/2;
            posParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;

            LinearLayout.LayoutParams negParams = (LinearLayout.LayoutParams) negButton.getLayoutParams();
            negParams.width = parent.getWidth()/2;
            negParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;

            posButton.setTextColor(Color.parseColor("#222222"));
            negButton.setTextColor(Color.parseColor("#222222"));
            posButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            negButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        }
    });

答案 1 :(得分:0)

您需要实施OnShowListener并覆盖其onShow(DialogInterface dialog)方法,以根据OK宽度设置CANCELAlertDialog's按钮的宽度

Android文档说:

  

<强> OnShowListener:

     

用于允许对话框的创建者在何时运行某些代码的接口   显示对话框。

     

public abstract void onShow(DialogInterface对话框)

     

显示对话框时将调用此方法。

     

参数: 对话框 - 显示的对话框将传递给方法。

您可以在此行之后设置此代码:

AlertDialog alertD = alertDialogBuilder.create();

这是代码:

alertD.setOnShowListener(new OnShowListener() {

    @Override
    public void onShow(DialogInterface d) {
        Button posButton = alertD.getButton(DialogInterface.BUTTON_POSITIVE);
        Button negButton = alertD.getButton(DialogInterface.BUTTON_NEGATIVE);

        LayoutParams posParams = (LayoutParams) posButton.getLayoutParams();
        posParams.weight = 1;
        posParams.width = LayoutParams.MATCH_PARENT;

        LayoutParams negParams = (LayoutParams) negButton.getLayoutParams();
        negParams.weight = 1;
        negParams.width = LayoutParams.MATCH_PARENT;

        posButton.setLayoutParams(posParams);
        negButton.setLayoutParams(negParams);
    }
});

我希望它可以帮到你。

答案 2 :(得分:0)

这是一个Kotlin示例。

注意:AlertDialog布局中有一个Space,其中有一个layout_weight = 1.0f,因此,为每个对话框按钮设置一个很高的weight会使分隔符缩至最小。或者,您可以找到它(R.id.spacer)并将其隐藏。

dialog.setOnShowListener {
                dialog.getButton(DialogInterface.BUTTON_POSITIVE)?.apply {
                    updateLayoutParams<LinearLayout.LayoutParams> {
                        width = LinearLayout.LayoutParams.WRAP_CONTENT
                        weight = 100000.0f
                    }
                }
                dialog.getButton(DialogInterface.BUTTON_NEGATIVE)?.apply {
                    updateLayoutParams<LinearLayout.LayoutParams> {
                        width = LinearLayout.LayoutParams.WRAP_CONTENT
                        weight = 100000.0f
                    }
                }
            }

            dialog.show()