自定义AlertDialog

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. }





posted @ 2015-05-23 18:36  咖啡馆的水果拼盘  阅读(153)  评论(0编辑  收藏  举报