Android自定义对话框(Dialog)位置,大小

以你之姓@ 2024-02-17 17:32 13阅读 0赞
  1. import android.app.Activity;
  2. import android.app.Dialog;
  3. import android.os.Bundle;
  4. import android.view.Gravity;
  5. import android.view.Window;
  6. import android.view.WindowManager;
  7. public class DialogDemoActivity extends Activity {
  8. /** Called when the activity is first created. */
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.main);
  13. Dialog dialog = new Dialog(this);
  14. // setContentView可以设置为一个View也可以简单地指定资源ID
  15. // LayoutInflater
  16. // li=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
  17. // View v=li.inflate(R.layout.dialog_layout, null);
  18. // dialog.setContentView(v);
  19. dialog.setContentView(R.layout.dialog_layout);
  20. dialog.setTitle("Custom Dialog");
  21. /*
  22. * 获取圣诞框的窗口对象及参数对象以修改对话框的布局设置,
  23. * 可以直接调用getWindow(),表示获得这个Activity的Window
  24. * 对象,这样这可以以同样的方式改变这个Activity的属性.
  25. */
  26. Window dialogWindow = dialog.getWindow();
  27. WindowManager.LayoutParams lp = dialogWindow.getAttributes();
  28. dialogWindow.setGravity(Gravity.LEFT | Gravity.TOP);
  29. /*
  30. * lp.x与lp.y表示相对于原始位置的偏移.
  31. * 当参数值包含Gravity.LEFT时,对话框出现在左边,所以lp.x就表示相对左边的偏移,负值忽略.
  32. * 当参数值包含Gravity.RIGHT时,对话框出现在右边,所以lp.x就表示相对右边的偏移,负值忽略.
  33. * 当参数值包含Gravity.TOP时,对话框出现在上边,所以lp.y就表示相对上边的偏移,负值忽略.
  34. * 当参数值包含Gravity.BOTTOM时,对话框出现在下边,所以lp.y就表示相对下边的偏移,负值忽略.
  35. * 当参数值包含Gravity.CENTER_HORIZONTAL时
  36. * ,对话框水平居中,所以lp.x就表示在水平居中的位置移动lp.x像素,正值向右移动,负值向左移动.
  37. * 当参数值包含Gravity.CENTER_VERTICAL时
  38. * ,对话框垂直居中,所以lp.y就表示在垂直居中的位置移动lp.y像素,正值向右移动,负值向左移动.
  39. * gravity的默认值为Gravity.CENTER,即Gravity.CENTER_HORIZONTAL |
  40. * Gravity.CENTER_VERTICAL.
  41. *
  42. * 本来setGravity的参数值为Gravity.LEFT | Gravity.TOP时对话框应出现在程序的左上角,但在
  43. * 我手机上测试时发现距左边与上边都有一小段距离,而且垂直坐标把程序标题栏也计算在内了,
  44. * Gravity.LEFT, Gravity.TOP, Gravity.BOTTOM与Gravity.RIGHT都是如此,据边界有一小段距离
  45. */
  46. lp.x = 100; // 新位置X坐标
  47. lp.y = 100; // 新位置Y坐标
  48. lp.width = 300; // 宽度
  49. lp.height = 300; // 高度
  50. lp.alpha = 0.7f; // 透明度
  51. // 当Window的Attributes改变时系统会调用此函数,可以直接调用以应用上面对窗口参数的更改,也可以用setAttributes
  52. // dialog.onWindowAttributesChanged(lp);
  53. dialogWindow.setAttributes(lp);
  54. /*
  55. * 将对话框的大小按屏幕大小的百分比设置
  56. */
  57. // WindowManager m = getWindowManager();
  58. // Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
  59. // WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
  60. // p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6
  61. // p.width = (int) (d.getWidth() * 0.65); // 宽度设置为屏幕的0.65
  62. // dialogWindow.setAttributes(p);
  63. dialog.show();
  64. }
  65. }
  66. <?xml version="1.0" encoding="utf-8"?>
  67. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  68. android:layout_width="fill_parent"
  69. android:layout_height="fill_parent"
  70. android:background="#00FF00"
  71. android:orientation="vertical" >
  72. <TextView
  73. android:layout_width="fill_parent"
  74. android:layout_height="wrap_content"
  75. android:text="@string/hello" />
  76. </LinearLayout>
  77. <?xml version="1.0" encoding="utf-8"?>
  78. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  79. android:id="@+id/layout_root"
  80. android:layout_width="fill_parent"
  81. android:layout_height="fill_parent"
  82. android:orientation="horizontal"
  83. android:padding="10dp" >
  84. <ImageView
  85. android:id="@+id/image"
  86. android:layout_width="wrap_content"
  87. android:layout_height="wrap_content"
  88. android:layout_marginRight="10dp"
  89. android:src="@drawable/ic_launcher" />
  90. <TextView
  91. android:id="@+id/text"
  92. android:layout_width="wrap_content"
  93. android:layout_height="wrap_content"
  94. android:text="A Dialog"
  95. android:textColor="#FFF" />
  96. </LinearLayout>

发表评论

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

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

相关阅读