自定义AlertDialog

本文介绍了一种自定义原生Dialog的方法,并提供了详细的XML布局文件及对应的Java代码实现,展示了如何通过LayoutInflater加载布局文件并设置Dialog的显示样式。
Demo:DefineDialog
原生Dialog:DialogTest  % dialog
XML文件:
 
    
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout 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="30dp"
  6. android:background="#FFFFFF"
  7. android:orientation="vertical" >
  8. <RelativeLayout
  9. android:id="@+id/rl_dialog_content"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:layout_marginBottom="30dp" >
  13. <TextView
  14. android:id="@+id/textView1"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:layout_alignParentLeft="true"
  18. android:layout_alignParentTop="true"
  19. android:layout_marginTop="10dp"
  20. android:gravity="center"
  21. android:maxLines="5"
  22. android:text="温馨提示"
  23. android:layout_marginBottom="20dp"
  24. android:textColor="#3f3f3f"
  25. android:textSize="20sp" />
  26. <TextView
  27. android:id="@+id/dialog_text"
  28. android:layout_width="fill_parent"
  29. android:layout_height="wrap_content"
  30. android:layout_below="@+id/textView1"
  31. android:layout_centerHorizontal="true"
  32. android:gravity="center"
  33. android:maxLines="5"
  34. android:paddingLeft="10dp"
  35. android:paddingRight="10dp"
  36. android:text="亲,你今天20次的寻宝机会已用完,可通过藏宝来获得寻宝机会哦!"
  37. android:textColor="#666667"
  38. android:textSize="16sp" />
  39. </RelativeLayout>
  40. <LinearLayout
  41. android:layout_width="match_parent"
  42. android:layout_height="30dp"
  43. android:layout_below="@id/rl_dialog_content"
  44. android:orientation="horizontal" >
  45. <TextView
  46. android:id="@+id/dialog_cancel"
  47. android:layout_width="1dp"
  48. android:layout_height="match_parent"
  49. android:layout_weight="1"
  50. android:clickable="true"
  51. android:gravity="center"
  52. android:text="关注" />
  53. <LinearLayout
  54. android:layout_width="0.5dp"
  55. android:layout_height="match_parent"
  56. android:background="#dfdfdf" >
  57. </LinearLayout>
  58. <TextView
  59. android:id="@+id/dialog_ok"
  60. android:layout_width="1dp"
  61. android:layout_height="match_parent"
  62. android:layout_weight="1"
  63. android:gravity="center"
  64. android:text="发消息" />
  65. </LinearLayout>
  66. </LinearLayout>


Java:
 
   
  1. public class MainActivity extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. LayoutInflater inflaterDl = LayoutInflater.from(this);
  7. LinearLayout layout = (LinearLayout)inflaterDl.inflate(R.layout.dialog_xml, null );
  8. //对话框
  9. final Dialog dialog = new AlertDialog.Builder(MainActivity.this).create();
  10. dialog.show();
  11. WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
  12. params.width = 550;
  13. params.height = 350 ;
  14. dialog.getWindow().setAttributes(params);
  15. dialog.getWindow().setContentView(layout);
  16. //取消按钮
  17. TextView btnCancel = (TextView) layout.findViewById(R.id.dialog_cancel);
  18. btnCancel.setOnClickListener(new OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21. Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_SHORT).show();
  22. }
  23. });
  24. //确定按钮
  25. TextView btnOK = (TextView) layout.findViewById(R.id.dialog_ok);
  26. btnOK.setOnClickListener(new OnClickListener() {
  27. @Override
  28. public void onClick(View v) {
  29. Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();
  30. }
  31. });
  32. }
  33. }





转载于:https://www.cnblogs.com/fruitbolgs/p/4524720.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值