原文地址 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的布局View
- private View allMsgView;
- // 通过LayoutInflater找到改布局
- allMsgView = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.dialog_all_pre_entry_msg, null);
- //创建Dialog
- allMsg = 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的行了:
- /**
- * 按钮监听
- */
- @Override
- public void onClick(View v)
- {
- switch (v.getId())
- {
- // dialog的图片取消button
- case R.id.dialog_pre_entry_close:
- allMsg.dismiss();
- break;
- default:
- break;
- }
- }
布局效果图: