AlertDialog使用自定义的布局

来源:互联网 发布:中国网络安全法 编辑:程序博客网 时间:2024/05/09 00:40

Android经常用到对话框—-AlertDialog, 效果图如下:

这里写图片描述

这是使用系统自带的样式, 使用了建造者模式, Builder添加自己需要显示的item.

代码如下:

    new AlertDialog.Builder(this).setTitle("这是标题").setMessage("这是内容").setPositiveButton("确定", new DialogInterface.OnClickListener() {        @Override        public void onClick(DialogInterface dialogInterface, int i) {            // TODO:   点确定的操作            Toast.makeText(TestActivity.this,"确定",Toast.LENGTH_SHORT).show();        }    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {        @Override        public void onClick(DialogInterface dialogInterface, int i) {            // TODO:  点取消的操作            Toast.makeText(TestActivity.this,"取消",Toast.LENGTH_SHORT).show();        }    }).create().show();}

但是如果要使用自义的样式就得使用到一个方法: setView(View view).

效果图如下:

这里写图片描述
下面是代码:

public class TestActivity extends AppCompatActivity implements View.OnClickListener {    private AlertDialog mDialog;    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findViewById(R.id.btn).setOnClickListener(this);    }    @Override    public void onClick(View v) {        final AlertDialog.Builder builder = new AlertDialog.Builder(this);        View view = View.inflate(this, R.layout.update_dialog, null);        ((TextView) view.findViewById(R.id.title_tv)).setText("我是标题");        ((TextView) view.findViewById(R.id.message_tv)).setText("我是内容,虽然样式很丑,但是具体的用法可以使用你自己的漂亮一点的布局");        view.findViewById(R.id.not_btn).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                mDialog.dismiss();            }        });        view.findViewById(R.id.ok_btn).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // TODO:  点确定的操作            }        });        mDialog = builder.setView(view).create();        mDialog.show();    }}

布局代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:layout_width="180dp"              android:layout_height="wrap_content"              android:orientation="vertical">    <TextView        android:id="@+id/title_tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:padding="10dp"    />    <View        android:layout_width="match_parent"        android:layout_height="1dp"        android:layout_below="@id/title_tv"        android:background="#66515151"/>    <TextView        android:id="@+id/message_tv"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ellipsize="end"        android:maxLines="4"        android:padding="5dp"/>    <View        android:layout_width="match_parent"        android:layout_height="1dp"        android:background="#66515151"/>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true">        <Button            android:id="@+id/not_btn"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="取消"            android:background="#fff"        />        <View            android:layout_width="1dp"            android:layout_height="match_parent"            android:background="#66505050"/>        <Button            android:id="@+id/ok_btn"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="确定"            android:background="#fff"/>    </LinearLayout></LinearLayout>
1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩喜欢偷钱怎么办 小孩老是偷钱怎么办 如果孩子偷钱怎么办 小孩在家偷钱怎么办 买了西晒房怎么办 乳胶漆有打磨痕迹怎么办 墙上乳胶漆用水擦完太亮了怎么办 外墙腻子不好打磨怎么办 腻子打磨的灰尘怎么办 家里的壁纸脏了怎么办 家装用冷风管冬天取暖怎么办? 房子装修冷色系怎么办 高血压突然变低血压怎么办 玩单机游戏卡顿怎么办 老人血压太低怎么办 老年人血压太低怎么办 觉总是不够睡怎么办 老年人气不够用怎么办 卧室晒不到太阳怎么办 农村房子墙角受潮怎么办 房间里面有虫子怎么办 墙壁发霉生虫子怎么办 墙壁发霉有虫子怎么办 墙面发霉有虫子怎么办 白漆墙面脏了怎么办 房间贴完墙墙面发霉怎么办? 家里的墙壁发霉怎么办 黑豆生虫了怎么办 家具有小虫子怎么办 店铺对面有镜子怎么办 房间里有蛆虫怎么办 房间里有虫子怎么办 小宝宝发烧了怎么办啊 小孩不盖被子怎么办 孩子不爱盖被子怎么办 宝宝不喜欢盖被子怎么办 被单上染上颜色怎么办 白漆不够白怎么办 衣柜推拉门门缝宽怎么办 推拉门时声音大怎么办 电子门坏掉了怎么办