AlertDialog使用自定义的布局

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

AlertDialog使用自定义的布局_第1张图片

这是使用系统自带的样式, 使用了建造者模式, 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).

效果图如下:

AlertDialog使用自定义的布局_第2张图片
下面是代码:

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();
    }
}

布局代码:


<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>

你可能感兴趣的:(android)