【Android】UI组件之AlertDialog的自定义实现

使用LayoutInflater把自定义的view放入AlertDialog中

新建一个button5
<Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示自定义dialog"
        android:layout_below="@id/button4"
        android:onClick="test5"
        />
新建点击事件,点击button5后,可弹出自定义对话框login
public void test5(View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("登录窗口");
       View login = this.getLayoutInflater().inflate(R.layout.login,null);

        builder.setView(login);

        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
新建login.xml,即为对话弹出的画面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户名:"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="13"
        android:layout_toRightOf="@id/textView1"
        />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码:"
        android:layout_below="@id/textView1"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="30dp"/>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1"
        android:layout_toRightOf="@id/textView2"
        android:ems="13"
        android:layout_marginTop="10dp"
        android:layout_alignLeft="@id/editText1"/>



    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注册"
        android:layout_below="@id/textView2"
        android:layout_alignLeft="@id/editText1"
        android:layout_marginTop="30dp"
        />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        android:layout_below="@id/textView2"
        android:layout_toRightOf="@id/button7"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="30dp"
        />

</RelativeLayout>
最后的效果图

请添加图片描述
请添加图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值