關於AlertDialog.Builder的自定義

最近在網上找了一些關於AlertDialog.Builder自定義佈局的博客帖子之類的文章,發現說的都不怎麼滿足自己的需求,所以稍稍整合了一下:

 

由於項目需求,需要讓builder.setTitle(title)居中,並且字體大小和顏色也需要有相應的改變。

首先,自定義佈局,只是這個佈局沒有包括取消確定

 

 

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f5f5f5"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/dialog_red_title"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:padding="5dp"
            android:text="提示"
            android:textColor="#ff0000"
            android:textSize="25sp"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="10dp"
            android:visibility="visible" />

        <TextView
            android:id="@+id/dialog_red_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="30dp"
            android:gravity="center"
            android:padding="5dp"
            android:textSize="18sp"
            android:visibility="gone" />

    </LinearLayout>

</LinearLayout></span>

 

 

在xml中只寫了title和message。

 

代碼實現如下:

 


 

 

 
final View layout = LayoutInflater.from(this).inflate(R.layout.alert_dialog_red, null);

final TextView tv = (TextView) layout.findViewById(R.id.dialog_red_title);
final TextView tm = (TextView) layout.findViewById(R.id.dialog_red_message);

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

                        tv.setText("恭喜  領取成功");
                        tm.setVisibility(View.VISIBLE);
                        tm.setText("已保存至“個人中心”——“我的紅包”");
                        builder.setView(layout);
                        builder.setPositiveButton("立即查看", new DialogInterface.OnClickListener() {

                          @Override
                          public void onClick(DialogInterface dialog, int which) {
                           //處理點擊事件
                          }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {

                          @Override
                          public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                          }
                        }).show();


        //也可以這樣寫
          tv.setText("不要急,您還沒登錄呢");
          tv.setPadding(0, 20, 0, 20);
          builder.setView(layout);
          builder.setPositiveButton("立即登陸", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
              Intent intent = new Intent(SecondRedActivity.this, MycenterLoginActivity.class);
              startActivity(intent);
            }
          }).setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
              dialog.cancel();
            }
          }).show();

 

 

效果圖:

 

 

 

 

 

 

18.5.23 更新

之前的dialog 還是有瑕疵,如全屏。。。

下面更新一波:完全自定義佈局,爽歪歪!!!

       View layout = (RelativeLayout)getActivity().getLayoutInflater().inflate(R.layout.dialog_svip,null);
        TextView tv = (TextView) layout.findViewById(R.id.tv_title_dialog);
        TextView tm = (TextView) layout.findViewById(R.id.tv_btn_dialog);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(layout);

        final AlertDialog dialog = builder.create();
        dialog.show();

        tm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
            }
        });

18.7.7 更新

習慣自定義xml文件來達到需要的佈局,最近項目自定義的時候,發現佈局的樣式不對,設置的圓角,背景透明,也沒效果,糾結了很久,然後自定義AlertDialog,發現問題並沒有解決,當然佈局還是同一個。

解決方法,及相關代碼:

             //彈出dialg
            View layout = (LinearLayout) getActivity().getLayoutInflater().inflate(R.layout.dialog_updata, null);
            TextView tvTitle = (TextView) layout.findViewById(R.id.tv_updata_title);
            TextView tvUpdata = (TextView) layout.findViewById(R.id.tv_updata_updata);
            ImageView tvClose = (ImageView) layout.findViewById(R.id.tv_updata_close);

            tvTitle.setText(appContent);

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setView(layout);
            builder.setCancelable(false);

            final AlertDialog dialog = builder.create();
            //去掉自定義dialog的白色背景
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

            dialog.show();
import android.support.v7.app.AlertDialog;

  記得包別導錯了

 

 

發佈了38 篇原創文章 · 獲贊 43 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章