自定义dialog,实现右上角显示一个控件按钮

原文地址 http://blog.csdn.net/BBLD_/article/details/27070531


这里是使用自定义dialog的布局实现,并去除原生dialog的标题。

以下是dialog布局的xml文件:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:layout_margin="50dp"  
  6.     android:background="@android:color/transparent"  
  7.     android:gravity="center" >  
  8.   
  9.     <LinearLayout  
  10.         android:id="@+id/LL_this"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_marginLeft="9dp"  
  14.         android:layout_marginRight="9dp"  
  15.         android:layout_marginTop="9dp"  
  16.         android:background="@drawable/rounded_background"  
  17.         android:orientation="vertical" >  
  18.   
  19.         <TableLayout  
  20.             android:layout_width="match_parent"  
  21.             android:layout_height="wrap_content"  
  22.             android:layout_marginLeft="20dp"  
  23.             android:layout_marginRight="20dp"  
  24.             android:layout_marginTop="30dp"  
  25.             android:shrinkColumns="1" >  
  26.   
  27.             <TableRow>  
  28.   
  29.                 <TextView  
  30.                     android:layout_width="wrap_content"  
  31.                     android:layout_height="wrap_content"  
  32.                     android:layout_marginTop="10dp"  
  33.                     android:text="课室: " />  
  34.   
  35.                 <TextView  
  36.                     android:id="@+id/txt_pre_entry_dialog_classroom"  
  37.                     android:layout_width="wrap_content"  
  38.                     android:layout_height="wrap_content"  
  39.                     android:layout_marginTop="10dp"  
  40.                     android:text="数据异常" />  
  41.             </TableRow>  
  42.   
  43.             <View  
  44.                 android:layout_width="match_parent"  
  45.                 android:layout_height="1dp"  
  46.                 android:background="@color/gray" />  
  47.   
  48.             <TableRow>  
  49.   
  50.                 <TextView  
  51.                     android:layout_width="wrap_content"  
  52.                     android:layout_height="wrap_content"  
  53.                     android:layout_marginTop="10dp"  
  54.                     android:text="老师: " />  
  55.   
  56.                 <TextView  
  57.                     android:id="@+id/txt_pre_entry_dialog_teacher"  
  58.                     android:layout_width="wrap_content"  
  59.                     android:layout_height="wrap_content"  
  60.                     android:layout_marginTop="10dp"  
  61.                     android:text="数据异常" />  
  62.             </TableRow>  
  63.   
  64.             <View  
  65.                 android:layout_width="match_parent"  
  66.                 android:layout_height="1dp"  
  67.                 android:background="@color/gray" />  
  68.   
  69.             <TableRow>  
  70.   
  71.                 <TextView  
  72.                     android:layout_width="wrap_content"  
  73.                     android:layout_height="wrap_content"  
  74.                     android:layout_marginTop="10dp"  
  75.                     android:text="课程: " />  
  76.   
  77.                 <TextView  
  78.                     android:id="@+id/txt_pre_entry_dialog_course"  
  79.                     android:layout_width="wrap_content"  
  80.                     android:layout_height="wrap_content"  
  81.                     android:layout_marginTop="10dp"  
  82.                     android:text="数据异常" />  
  83.             </TableRow>  
  84.   
  85.             <View  
  86.                 android:layout_width="match_parent"  
  87.                 android:layout_height="1dp"  
  88.                 android:background="@color/gray" />  
  89.   
  90.             <TableRow>  
  91.   
  92.                 <TextView  
  93.                     android:layout_width="wrap_content"  
  94.                     android:layout_height="wrap_content"  
  95.                     android:layout_marginTop="10dp"  
  96.                     android:text="班级: " />  
  97.   
  98.                 <TextView  
  99.                     android:id="@+id/txt_pre_entry_dialog_classes"  
  100.                     android:layout_width="wrap_content"  
  101.                     android:layout_height="wrap_content"  
  102.                     android:layout_marginTop="10dp"  
  103.                     android:text="数据异常" />  
  104.             </TableRow>  
  105.   
  106.             <View  
  107.                 android:layout_width="match_parent"  
  108.                 android:layout_height="1dp"  
  109.                 android:layout_marginBottom="10dp"  
  110.                 android:background="@color/gray" />  
  111.         </TableLayout>  
  112.     </LinearLayout>  
  113.   
  114.     <ImageButton  
  115.         android:id="@+id/dialog_pre_entry_close"  
  116.         android:layout_width="wrap_content"  
  117.         android:layout_height="wrap_content"  
  118.         android:layout_alignParentRight="true"  
  119.         android:layout_alignParentTop="true"  
  120.         android:background="@drawable/cancel" />  
  121.   
  122. </RelativeLayout>  


要实现在右上角偏移突出显示一个关闭的Button,使用RelativeLayout方便点。同时第6、13、14、15行也起了作用,有什么作用效果大家动下手修改修改就知道了。


然后就要到代码里去设置dialog了,如下:

[java]  view plain copy
  1. private Dialog allMsg;  
  2. //Dialog的布局View  
  3. private View allMsgView;  
  4.   
  5. // 通过LayoutInflater找到改布局  
  6. allMsgView = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.dialog_all_pre_entry_msg, null);  
  7. //创建Dialog  
  8. allMsg = new AlertDialog.Builder(this).create();  
  9. //设置点击外边缘不消失,2.x的应该是默认不消失的  
  10. allMsg.setCanceledOnTouchOutside(false);  
  11. //findView布局里的控件  
  12. imgBtn_dialog = (ImageButton) allMsgView.findViewById(R.id.dialog_pre_entry_close);  
  13. imgBtn_dialog.setOnClickListener(this);  
  14.   
  15. txt_dialog_classroom = (TextView) allMsgView.findViewById(R.id.txt_pre_entry_dialog_classroom);  
  16. txt_dialog_course = (TextView) allMsgView.findViewById(R.id.txt_pre_entry_dialog_course);  
  17. txt_dialog_teacher = (TextView) allMsgView.findViewById(R.id.txt_pre_entry_dialog_teacher);  
  18. txt_dialog_classes = (TextView) allMsgView.findViewById(R.id.txt_pre_entry_dialog_classes);  

然后载你需要弹出的地方调用如下:

[java]  view plain copy
  1. //两句的顺序不能调换  
  2. allMsg.show();  
  3. allMsg.getWindow().setContentView((RelativeLayout) allMsgView);  

取消在关闭按钮的监听了关闭dialog的行了:

[java]  view plain copy
  1. /** 
  2.  * 按钮监听 
  3.  */  
  4. @Override  
  5. public void onClick(View v)  
  6. {  
  7.     switch (v.getId())  
  8.     {  
  9.     // dialog的图片取消button  
  10.     case R.id.dialog_pre_entry_close:  
  11.         allMsg.dismiss();  
  12.         break;  
  13.     default:  
  14.         break;  
  15.     }  
  16. }  


布局效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值