Android自定义Dialog(仿QQ同步助手退出对话框)

忘是亡心i 2022-06-06 09:35 307阅读 0赞

继承Dialog类就可以了,写写布局文件,写写style,就OK了。下面开始。

先上布局文件:

  1. <LinearLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. android:background="@drawable/bg_dialog_bottom" >
  7. <View
  8. android:layout_width="fill_parent"
  9. android:layout_height="20dip"
  10. />
  11. <TextView
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:gravity="center"
  15. android:text="退出确认"
  16. android:textSize="22sp"
  17. android:textColor="#000000"
  18. />
  19. <View
  20. android:layout_width="fill_parent"
  21. android:layout_height="30dip"
  22. />
  23. <TextView
  24. android:layout_width="fill_parent"
  25. android:layout_height="wrap_content"
  26. android:gravity="center"
  27. android:text="确定退出程序?"
  28. android:textSize="18sp"
  29. android:textColor="#2E2E2E"
  30. />
  31. <View
  32. android:layout_width="fill_parent"
  33. android:layout_height="20dip"
  34. />
  35. <LinearLayout
  36. android:layout_width="fill_parent"
  37. android:layout_height="wrap_content"
  38. android:gravity="center"
  39. android:orientation="horizontal" >
  40. <Button
  41. android:id="@+id/btOK"
  42. android:layout_width="120dip"
  43. android:layout_height="40dip"
  44. android:layout_marginRight="20dip"
  45. android:text="确定"
  46. android:textColor="#454545"
  47. android:background="@drawable/btn_dialog"
  48. />
  49. <Button
  50. android:id="@+id/btCancel"
  51. android:layout_width="120dip"
  52. android:layout_height="40dip"
  53. android:layout_marginLeft="20dip"
  54. android:text="取消"
  55. android:textColor="#454545"
  56. android:background="@drawable/btn_dialog"
  57. />
  58. </LinearLayout>
  59. <View
  60. android:layout_width="fill_parent"
  61. android:layout_height="30dip"
  62. />
  63. </LinearLayout>

然后写style

  1. <resources>
  2. <style name="MyDialog" parent="@android:Theme.Dialog">
  3. <item name="android:windowFrame">@null</item>
  4. <item name="android:windowNoTitle">true</item>
  5. <item name="android:windowIsFloating">true</item>
  6. <item name="android:windowContentOverlay">@null</item>
  7. </style>
  8. </resources>

扩展的Dialog类:

  1. public class MyDialog extends Dialog {
  2. private Button Ok,Cancel;
  3. private Context context;
  4. public MyDialog(Context context) {
  5. super(context);
  6. // TODO Auto-generated constructor stub
  7. this.context = context;
  8. }
  9. public MyDialog(Context context, int theme) {
  10. super(context, theme);
  11. this.context = context;
  12. }
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. // TODO Auto-generated method stub
  16. super.onCreate(savedInstanceState);
  17. this.setContentView(R.layout.dialog);
  18. Ok = (Button)findViewById(R.id.btOK);
  19. Cancel = (Button)findViewById(R.id.btCancel);
  20. Ok.setOnClickListener(new View.OnClickListener() {
  21. public void onClick(View v) {
  22. // TODO Auto-generated method stub
  23. Toast.makeText(context, "OK", Toast.LENGTH_SHORT).show();
  24. }
  25. });
  26. Cancel.setOnClickListener(new View.OnClickListener() {
  27. public void onClick(View v) {
  28. // TODO Auto-generated method stub
  29. Toast.makeText(context, "Cancel", Toast.LENGTH_SHORT).show();
  30. }
  31. });
  32. }
  33. }

对比一下效果:

1344433623_7535.png1344433635_6502.png

有点差别,一个是selector一个是style写的有点问题,继续研究下。

发表评论

表情:
评论列表 (有 0 条评论,307人围观)

还没有评论,来说两句吧...

相关阅读