国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程自定義對話框(Dialog)位置及大小的方法

2019-12-12 02:14:59
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程自定義對話框(Dialog)位置及大小的方法。分享給大家供大家參考,具體如下:

代碼:

package angel.devil;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 DialogDemoActivity extends Activity {  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    Dialog dialog = new Dialog(this);    // setContentView可以設置為一個View也可以簡單地指定資源ID    // LayoutInflater    // li=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);    // View v=li.inflate(R.layout.dialog_layout, null);    // dialog.setContentView(v);    dialog.setContentView(R.layout.dialog_layout);    dialog.setTitle("Custom Dialog");    /*     * 獲取圣誕框的窗口對象及參數對象以修改對話框的布局設置,     * 可以直接調用getWindow(),表示獲得這個Activity的Window     * 對象,這樣這可以以同樣的方式改變這個Activity的屬性.     */    Window dialogWindow = dialog.getWindow();    WindowManager.LayoutParams lp = dialogWindow.getAttributes();    dialogWindow.setGravity(Gravity.LEFT | 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.     *     * 本來setGravity的參數值為Gravity.LEFT | Gravity.TOP時對話框應出現在程序的左上角,但在     * 我手機上測試時發現距左邊與上邊都有一小段距離,而且垂直坐標把程序標題欄也計算在內了,     * Gravity.LEFT, Gravity.TOP, Gravity.BOTTOM與Gravity.RIGHT都是如此,據邊界有一小段距離     */    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 = dialogWindow.getAttributes(); // 獲取對話框當前的參數值//    p.height = (int) (d.getHeight() * 0.6); // 高度設置為屏幕的0.6//    p.width = (int) (d.getWidth() * 0.65); // 寬度設置為屏幕的0.65//    dialogWindow.setAttributes(p);    dialog.show();  }}

布局文件:

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:background="#00FF00"  android:orientation="vertical" >  <TextView    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="@string/hello" /></LinearLayout>

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>

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辉县市| 新和县| 文成县| 西宁市| 余江县| 罗江县| 宁晋县| 舞阳县| 上高县| 景宁| 萝北县| 平南县| 东辽县| 舞钢市| 双桥区| 房产| 姜堰市| 通辽市| 论坛| 秀山| 获嘉县| 万山特区| 吉木萨尔县| 佛山市| 武穴市| 西和县| 加查县| 嘉义市| 嘉荫县| 寿宁县| 青浦区| 嵩明县| 阜新市| 隆化县| 吉木萨尔县| 奉节县| 东海县| 清水河县| 互助| 南丹县| 延寿县|