系统自带的对话框dialog并不能满足更严格的开发需求,所以需要自定义对话框;
首先创建显示点击按钮布局文件:activity_main.xml
<?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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt"
android:text="弹出自定义对话框"
android:layout_marginTop="20dp"
android:layout_gravity="center"
/>
</LinearLayout>
接下来创建dialog.xml,自定义对话框的布局:
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content

本文介绍了如何在Android开发中自定义对话框以满足特定需求。通过创建布局文件dialog.xml,编写逻辑处理类CommonDialog继承自AlertDialog,并重写onCreate方法来设置布局和控件。此外,还详细说明了如何初始化界面、设置控件监听、刷新视图以及定义按钮接口,以供MainAcitvity调用并展示自定义对话框。
最低0.47元/天 解锁文章
2000

被折叠的 条评论
为什么被折叠?



