如何在警告对话框中自定义负面和正面按钮?

时间:2017-10-31 10:44:04

标签: android android-layout

我是android开发的新手,对此有疑问(截图如下)。

enter image description here

是,否按钮栏的背景颜色为深灰色。我已经完成了所有关于android开发的知识,但没有弄清楚如何做到这一点。  **任何人都可以告诉我应该如何定制它即。背景颜色,水平对齐等。**

这是我的代码。

custom_dialog.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:textColor="#ffffff"
        android:textSize="45sp"
        android:paddingTop="5dp"
        android:paddingLeft="10dp"
        android:paddingBottom="5dp"
        android:paddingRight="10dp"
        android:gravity="center_horizontal"/>
    <RelativeLayout
        android:id="@+id/body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:paddingTop="5dp"
        android:paddingLeft="10dp"
        android:paddingBottom="5dp"
        android:paddingRight="10dp"
        android:layout_below="@+id/title"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TextView
            android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="You are leaving from the app."
            android:textColor="#000000"
            android:textSize="25sp"
            android:gravity="center_horizontal"/>
        <TextView
            android:id="@+id/text2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="20dp"
            android:layout_below="@+id/text1"
            android:text="Are you sure."
            android:textColor="#000000"
            android:textSize="25sp"
            android:gravity="center_horizontal"/>

    </RelativeLayout>

</RelativeLayout>

homeactivity.java

public void onBackPressed() {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                HomeActivity.this);


        View view = LayoutInflater.from(HomeActivity.this).inflate(R.layout.custom_dialog,null);
        TextView title = (TextView) view.findViewById(R.id.title);
        title.setText("Leaving?");

        alertDialogBuilder
                .setCancelable(false)
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        HomeActivity.this.finish();
                    }
                })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.setView(view);
        alertDialog.show();
    }

帮助我任何人。提前谢谢。

2 个答案:

答案 0 :(得分:1)

予。为对话框背景声明自定义drawable.xml。

<form>
<select class="search-category-dropdown" onchange="if (this.value) window.location.href=this.value">
<option value="">1</option>
<option value="">2</option>
</select>
</form>

II。在styles.xml文件中声明自定义样式。

<?xml version="1.0" encoding="utf-8"?>
<!-- From: support/v7/appcompat/res/drawable/abc_dialog_material_background_light.xml -->
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="16dp"
    android:insetTop="16dp"
    android:insetRight="16dp"
    android:insetBottom="16dp">

    <shape android:shape="rectangle">
        <corners android:radius="2dp" />
        <solid android:color="@color/indigo" />
    </shape>

</inset>

III。创建对话框并使用样式作为AlertDialog.Builder中的参数。

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <!--buttons color-->
    <item name="colorAccent">@color/pink</item>
    <!--title and message color-->
    <item name="android:textColorPrimary">@android:color/white</item>
    <!--dialog background-->
    <item name="android:windowBackground">@drawable/drawable</item>
</style>

答案 1 :(得分:0)

创建精确的xml,如图像中所示,包括标题和按钮,而不是添加警告对话框:

final Dialog dialog = new Dialog(MainActivity.this);
            dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog1.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            dialog1.setContentView(R.layout.custom_dialog);
            dialog1.setCancelable(false);

            dialog1.show();
btn_yes = (Button)dialog.findViewById(R.id.btn_yes);
btn_no = (Button)dialog.findViewById(R.id.btn_no);
            btn_yes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dialog1.dismiss();
                    finish();

                }
            });

 btn_no.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dialog1.dismiss();
                    finish();

                }
            });

            dialog1.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    // TODO Auto-generated method stub
                    if (keyCode == KeyEvent.KEYCODE_BACK) {

                        finish();

                    }

                    return true;
                }
            });

        }