如何自定义弹出通知?

时间:2017-09-19 20:15:44

标签: android push-notification popupwindow android-imagebutton

单击图像按钮时,弹出弹出通知。如何自定义" ok"和"取消"按钮而不是使用按钮的默认外观,我想使用我自己的自定义ImageButtons作为"确定"和"取消"。

这是我弹出通知的代码。

public class Notifications extends AppCompatActivity {


ImageButton Notifications;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notifications);


    Notifications = (ImageButton) findViewById(R.id.AllowNotifications);


Notifications.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {

        AlertDialog.Builder builder = new AlertDialog.Builder(Notifications.this);

        builder.setCancelable(false); //False= ONLY way to exist notification is by clicking NO
        //True= Exit notification by clicking anywhere on screen outside of notification box.

        builder.setTitle("Here is the alert dialog");
        builder.setMessage("Here is my message thing");

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int WhichButton) {
                dialog.cancel();
            }
        });


        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        builder.show();

                }
           });
 }
}

这是使用上述代码的默认弹出式通知:

enter image description here

所以没有" ok"和"取消"在红色,我想把"确定"和"取消"作为我自己的自定义图像按钮,我想要将颜色从红色更改为其他颜色。如何在弹出通知中执行此操作?

2 个答案:

答案 0 :(得分:1)

作为documentation says,在创建自定义布局会话中,您可以创建自定义布局并在对话框中对其进行充气。

要使用除Alertndialog.Builder创建的按钮之外的其他按钮,您需要处理它们的单击侦听器。

这是我为测试解决方案而创建的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:id="@+id/dialogTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Here is the alert dialog"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/dialogSubtitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Here is my message thing"/>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp">

        <Button
            android:id="@+id/positiveButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:background="@android:color/transparent"
            android:text="OK"
            android:textColor="@android:color/holo_red_light"/>

        <Button
            android:id="@+id/negativeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="40dp"
            android:layout_toStartOf="@id/positiveButton"
            android:background="@android:color/transparent"
            android:text="Cancel"
            android:textColor="@android:color/holo_red_light"/>

    </RelativeLayout>

</LinearLayout>

使代码运行的代码:

LayoutInflater layoutInflater = LayoutInflater.from(this);
        View promptView = layoutInflater.inflate(R.layout.test, null);

        final AlertDialog alertD = new AlertDialog.Builder(this).create();

        TextView title = (TextView) promptView.findViewById(R.id.dialogTitle);
        TextView subtitle = (TextView) promptView.findViewById(R.id.dialogSubtitle);

        title.setText("My new Custom Dialog");
        subtitle.setText("With everything that I want");

        Button positive = (Button) promptView.findViewById(R.id.positiveButton);
        Button negativeButton = (Button) promptView.findViewById(R.id.negativeButton);

        positive.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // btnAdd1 has been clicked
            }
        });

        negativeButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // btnAdd2 has been clicked
            }
        });

        alertD.setView(promptView);
        alertD.show();

这是我手机中的样子截图。您可以随意更改布局,以更好地满足您的需求。

Example of Dialog

感谢Vikram在this answers for other question中解释得非常好,但我认为针对您的问题的特定代码会更好。

答案 1 :(得分:0)

如果你想自定义所有内容,对话框的外观,添加你自己的按钮,TextViews等 - 你需要创建一个扩展DialogFragment并实现View.OnClickListener的类,你需要创建自己的两个自定义布局制作按钮。给它们ID并设置OnClickListeners

输入:https://developer.android.com/reference/android/app/DialogFragment.html

public static class MyDialogFragment extends DialogFragment implements View.OnClickListener {
    static MyDialogFragment newInstance() {
        return new MyDialogFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.dialog_fragment, container, false);
        v.findViewById(R.id.btn_ok).setOnClickListener(this);
        return v;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_ok:
                // do something
                break;
            default:
                break;
        }
    }
}

从您的活动开始:

void showDialog() {
    // Create the fragment and show it as a dialog.
    DialogFragment newFragment = MyDialogFragment.newInstance();
    newFragment.show(getFragmentManager(), "dialog");
}