[Android] Android自訂對話方塊(Dialog)位置,大小

來源:互聯網
上載者:User

文章是搜出來的,原文出處:http://www.cnblogs.com/angeldevil/archive/2012/03/31/2426242.html

本文的重點在於自訂Dialog位置時,WindowManager.LayoutParams的x/y值是與Gravity關聯的相對值。並在原文基礎上補充一點為:x/y的值若超出螢幕範圍,則Dialog不會顯示不全或出現在螢幕之外,仍會在螢幕的邊緣處完整的顯示出來。

對於原文中所提出的問題:"距邊界有一小段距離"。則是由於Dialog的背景圖片有半透明的過渡地區所致,這些過渡地區已經是Dialog本身的一部分,看一下本人的曆史文章“Android有趣的全透明效果--Activity及Dialog的全透明(附android系統內建表徵圖大全)”中的第二部分“2.Dialog全透明”中的第一張圖就能解釋了。

代碼如下:

package lab.sodino.dialoglab;import android.app.Activity;import android.app.Dialog;import android.os.Bundle;import android.view.Gravity;import android.view.Window;import android.view.WindowManager;public class ActDialog extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.act_dialog);Dialog dialog = new Dialog(this);dialog.setContentView(R.layout.dialog_layout);dialog.setTitle("Custom Dialog");dialog.show();/* * 擷取聖誕框的視窗對象及參數對象以修改對話方塊的布局設定, 可以直接調用getWindow(),表示獲得這個Activity的Window * 對象,這樣這可以以同樣的方式改變這個Activity的屬性. */Window dialogWindow = dialog.getWindow();WindowManager.LayoutParams lp = dialogWindow.getAttributes();dialogWindow.setGravity(Gravity.CENTER);/* * lp.x與lp.y表示相對於原始位置的位移. * 當參數值包含Gravity.LEFT時,對話方塊出現在左邊,所以lp.x就表示相對左邊的位移,負值忽略. * 當參數值包含Gravity.RIGHT時,對話方塊出現在右邊,所以lp.x就表示相對右邊的位移,負值忽略. * 當參數值包含Gravity.TOP時,對話方塊出現在上邊,所以lp.y就表示相對上邊的位移,負值忽略. * 當參數值包含Gravity.BOTTOM時,對話方塊出現在下邊,所以lp.y就表示相對下邊的位移,負值忽略. * 當參數值包含Gravity.CENTER_HORIZONTAL時 * ,對話方塊水平置中,所以lp.x就表示在水平置中的位置移動lp.x像素,正值向右移動,負值向左移動. * 當參數值包含Gravity.CENTER_VERTICAL時 * ,對話方塊垂直置中,所以lp.y就表示在垂直置中的位置移動lp.y像素,正值向右移動,負值向左移動. * gravity的預設值為Gravity.CENTER,即Gravity.CENTER_HORIZONTAL * |Gravity.CENTER_VERTICAL. *  * 本來setGravity的參數值為Gravity.LEFT | Gravity.TOP時對話方塊應出現在程式的左上方,但在 * 我手機上測試時發現距左邊與上邊都有一小段距離,而且垂直座標把程式標題列也計算在內了, Gravity.LEFT, Gravity.TOP, * Gravity.BOTTOM與Gravity.RIGHT都是如此,距邊界有一小段距離 */lp.x = 500; // 新位置X座標lp.y = 200; // 新位置Y座標lp.width = 300; // 寬度lp.height = 300; // 高度lp.alpha = 0.1f; // 透明度// 當Window的Attributes改變時系統會調用此函數,可以直接調用以應用上面對視窗參數的更改,也可以用setAttributes// dialog.onWindowAttributesChanged(lp);dialogWindow.setAttributes(lp);}}

dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/layout_root"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="horizontal"    android:padding="10dp" >    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginRight="10dp"        android:src="@drawable/ic_launcher" />    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="A Dialog"        android:textColor="#FFF" />        </LinearLayout>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.