1.模版例子
這個例子是網上找的,很明確的介紹了如何控制Dialog大小,位置和透明度等值的設置。它的主要思路是通過getWindow獲取Dialog的窗口,然后就可以設置Dialog的寬高和位置等參數的值。
package com.lgy.test;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
/**
* @author LGY
* http://blog.csdn.net/misly_vinky/article/details/19109517
*/
public class TemplateDialog extends Dialog{
public TemplateDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init()
{
/*
* 獲取對話框的窗口對象及參數對象以修改對話框的布局設置,
* 可以直接調用getWindow(),表示獲得這個Activity的Window
* 對象,這樣這可以以同樣的方式改變這個Activity的屬性.
*/
Window dialogWindow = getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
/*
* 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.
*
*/
lp.x = 100; // 新位置X坐標
lp.y = 100; // 新位置Y坐標
lp.width = 300; // 寬度
lp.height = 300; // 高度
lp.alpha = 0.7f; // 透明度
// 當Window的Attributes改變時系統會調用此函數,可以直接調用以應用上面對窗口參數的更改,也可以用setAttributes
// dialog.onWindowAttributesChanged(lp);
dialogWindow.setAttributes(lp);
/*
* 將對話框的大小按屏幕大小的百分比設置
*/
// WindowManager m = getWindowManager();
// Display d = m.getDefaultDisplay(); // 獲取屏幕寬、高用
// WindowManager.LayoutParams p = getWindow().getAttributes(); // 獲取對話框當前的參數值
// p.height = (int) (d.getHeight() * 0.6); // 高度設置為屏幕的0.6
// p.width = (int) (d.getWidth() * 0.65); // 寬度設置為屏幕的0.65
// dialogWindow.setAttributes(p);
}
}
2.使用例子介紹
1)直接繼承Dialog,在dialog里的onCreate方法中設置上面的參數
public class MyDialog extends Dialog{
public MyDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_layout);
Window dialogWindow = getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
// lp.width = LayoutParams.MATCH_PARENT; // 寬度
// lp.height = LayoutParams.MATCH_PARENT; // 高度
lp.width = 300; // 寬度
lp.height = 400; // 高度
lp.alpha = 0.7f;
dialogWindow.setAttributes(lp);
}
}
2)創建一個Dialog對象后再使用這個對象實例來設置參數。
private void test2()
{
dialog2 = new Dialog(this);
dialog2.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog2.setContentView(R.layout.dialog_layout);
Window dialogWindow = dialog2.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
// lp.width = LayoutParams.MATCH_PARENT; // 寬度
// lp.height = LayoutParams.MATCH_PARENT; // 高度
lp.width = 300; // 寬度
lp.height = 400; // 高度
lp.alpha = 0.7f;
dialogWindow.setAttributes(lp);
}
3.參考文章
http://blog.csdn.net/misly_vinky/article/details/19109517