关于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;

  记得包别导错了

发布了40 篇原创文章 · 获赞 45 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/CGG92/article/details/51505453