AlertDialog栏中添加布局
首先AlertDialog是个对话框,但是怎么才能让这个对话框中的内容更加丰富呢? 可以先AlertDialog中添加一个布局;
首先顶一个AlertDialog文件
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//这里就弄 成一个可输入账号密码的吧
builder.setTitle("请输入初始账号密码");
然后定义一个叫layout的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账号"
android:id="@+id/ed1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ed2"
android:hint="请输入密码"
/>
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="是否显示密码"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="确定"
android:id="@+id/bt1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="结束"
android:id="@+id/bt2"/>
</LinearLayout>
</LinearLayout>
改变layout为view
//首先定义一个LayoutInglater变量;
LayoutInflater useLayout = LayoutInflater.from(this);
//在把它用inflate方法转化为view类型;
View view = (View) useLayout.inflate(R.layout.legin,null);
然后绑定
builder.setView(view);
builder.show();
好了就这样啦