【问题标题】:How to customize pop up notifications?如何自定义弹出通知?
【发布时间】:2017-09-19 20:15:44
【问题描述】:

当您单击图像按钮时,会弹出弹出通知。如何自定义“确定”和“取消”按钮,而不是使用按钮的默认外观,我想使用自己的自定义 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();

                }
           });
 }
}

这是上面代码的默认弹出通知:

因此,我想将“ok”和“cancel”作为我自己的自定义图像按钮,而不是红色的“ok”和“cancel”,我想将颜色从红色更改为别的东西。如何在弹出通知中执行此操作?

【问题讨论】:

    标签: android push-notification popupwindow android-imagebutton


    【解决方案1】:

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

    要使用 AlertDialog.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();
    

    这是我手机中的屏幕截图。随意更改布局以使其更适合您的需求。

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

    【讨论】:

      【解决方案2】:

      如果您想自定义所有内容、对话框的外观、添加您自己的按钮、TextView 等 - 您需要创建一个扩展 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");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-09
        • 1970-01-01
        • 2020-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多