Android(安卓)自定义dialog,实现右上角显示一个控件按钮
16lz
2021-01-26
转载请注明出处:http://blog.csdn.net/bbld_/article/details/27070531
这里是使用自定义dialog的布局实现,并去除原生dialog的标题。
以下是dialog布局的xml文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="50dp" android:background="@android:color/transparent" android:gravity="center" > <LinearLayout android:id="@+id/LL_this" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="9dp" android:layout_marginRight="9dp" android:layout_marginTop="9dp" android:background="@drawable/rounded_background" android:orientation="vertical" > <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="30dp" android:shrinkColumns="1" > <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="课室: " /> <TextView android:id="@+id/txt_pre_entry_dialog_classroom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="数据异常" /> </TableRow> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/gray" /> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="老师: " /> <TextView android:id="@+id/txt_pre_entry_dialog_teacher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="数据异常" /> </TableRow> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/gray" /> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="课程: " /> <TextView android:id="@+id/txt_pre_entry_dialog_course" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="数据异常" /> </TableRow> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/gray" /> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="班级: " /> <TextView android:id="@+id/txt_pre_entry_dialog_classes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="数据异常" /> </TableRow> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginBottom="10dp" android:background="@color/gray" /> </TableLayout> </LinearLayout> <ImageButton android:id="@+id/dialog_pre_entry_close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:background="@drawable/cancel" /></RelativeLayout>
这里我写了其它的一些表格布局的东西,所以看起来多了一点,其实起到做用的属性代码也就几行。因为要实现在右上角偏移突出显示一个关闭的Button,这里就使用RelativeLayout了。上面的代码中第6、13、14、15行起了主要作用,有什么作用效果大家动下手修改修改就知道了。
然后就要到代码里去设置dialog了,如下:
private Dialog allMsg;//Dialog的布局Viewprivate View allMsgView;// 通过LayoutInflater找到改布局allMsgView = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.dialog_all_pre_entry_msg, null);//创建DialogallMsg = new AlertDialog.Builder(this).create();//设置点击外边缘不消失,2.x的应该是默认不消失的allMsg.setCanceledOnTouchOutside(false);//findView布局里的控件imgBtn_dialog = (ImageButton) allMsgView.findViewById(R.id.dialog_pre_entry_close);imgBtn_dialog.setOnClickListener(this);txt_dialog_classroom = (TextView) allMsgView.findViewById(R.id.txt_pre_entry_dialog_classroom);txt_dialog_course = (TextView) allMsgView.findViewById(R.id.txt_pre_entry_dialog_course);txt_dialog_teacher = (TextView) allMsgView.findViewById(R.id.txt_pre_entry_dialog_teacher);txt_dialog_classes = (TextView) allMsgView.findViewById(R.id.txt_pre_entry_dialog_classes);
然后在你需要弹出的地方调用如下:
//两句的顺序不能调换allMsg.show();allMsg.getWindow().setContentView((RelativeLayout) allMsgView);
取消显示,在关闭按钮的监听里关闭dialog就行了:
/** * 按钮监听 */@Overridepublic void onClick(View v){switch (v.getId()){// dialog的图片取消buttoncase R.id.dialog_pre_entry_close:allMsg.dismiss();break;default:break;}}
效果图:
demo下载地址:http://download.csdn.net/detail/bbld_/8118911
更多相关文章
- SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
- 一句话锁定MySQL数据占用元凶
- Android(安卓)Data Binding ——入门
- 使用Android(安卓)Studio开发一个简易的音乐播放器
- RecyclerView(三):LayoutManager职责及相关方法
- xUtils 源码解析
- Android中关于Volley的使用(二)加载Json数据
- Android中BaseFragment封装多状态视图显示
- android.view.WindowLeaked的解决办法 has leaked window androi