亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2017年08月16日 08:49:48   作者:fancylovejava  
這篇文章主要介紹了Android編程自定義對話框(Dialog)位置及大小的方法,涉及Android對話框的定義、功能、屬性及布局相關操作技巧,需要的朋友可以參考下

本文實例講述了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時對話框應出現在程序的左上角,但在
     * 我手機上測試時發(fā)現距左邊與上邊都有一小段距離,而且垂直坐標把程序標題欄也計算在內了,
     * 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改變時系統(tǒng)會調用此函數,可以直接調用以應用上面對窗口參數的更改,也可以用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開發(fā)入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結

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

相關文章

  • Android全面屏適配方法詳解

    Android全面屏適配方法詳解

    Apple一直在引領設計的潮流,自從 iPhone X 發(fā)布之后,各種異形屏、劉海屏也都出來,下面這篇文章主要給大家分享介紹了關于Android全面屏與異形(劉海)屏的適配教程,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-11-11
  • 詳解android6.0版本下懸浮窗實現

    詳解android6.0版本下懸浮窗實現

    這篇文章主要介紹了詳解android6.0版本下懸浮窗實現,懸浮窗在安卓中實現起來還是比較容易的,非常具有實用價值,需要的朋友可以參考下。
    2017-03-03
  • Android仿支付寶、京東的密碼鍵盤和輸入框

    Android仿支付寶、京東的密碼鍵盤和輸入框

    這篇文章主要介紹了利用Android如何模仿支付寶、京東的密碼鍵盤和輸入框,本文是將鍵盤和輸入框分開來寫,可以根據需求在各個地方使用,同時處理了大量邏輯,方便快速開發(fā)。感興趣的朋友們可以參考借鑒,下面來一起看看吧。
    2016-10-10
  • Android JSON數據與實體類之間的相互轉化(GSON的用法)

    Android JSON數據與實體類之間的相互轉化(GSON的用法)

    這篇文章主要介紹了Android JSON數據與實體類之間的相互轉化(GSON的用法),非常具有實用價值,需要的朋友可以參考下。
    2017-01-01
  • Android TreeView效果實現方法(附demo源碼下載)

    Android TreeView效果實現方法(附demo源碼下載)

    這篇文章主要介紹了Android TreeView效果實現方法,結合實例形式分析了Android TreeView效果的實現原理與具體技巧,并附帶demo源碼供讀者下載,需要的朋友可以參考下
    2016-02-02
  • Android SharedPreferences存取操作以及封裝詳解

    Android SharedPreferences存取操作以及封裝詳解

    SharedPreferences是安卓平臺上一個輕量級的存儲類,用來保存應用的一些常用配置,比如Activity狀態(tài),Activity暫停時,將此activity的狀態(tài)保存到SharedPereferences中;當Activity重載,系統(tǒng)回調方法onSaveInstanceState時,再從SharedPreferences中將值取出
    2021-11-11
  • Android圖片上傳實現預覽效果

    Android圖片上傳實現預覽效果

    這篇文章主要介紹了Android圖片上傳實現預覽效果的相關資料,需要的朋友可以參考下
    2016-01-01
  • Android中處理apple-touch-icon詳解

    Android中處理apple-touch-icon詳解

    這篇文章主要介紹了Android中處理apple-touch-icon詳解,在Android如果想把網頁放到桌面上,也需要使用Touch Icon圖標才可實現,本文即講解Android中處理Touch Icon的一些知識,需要的朋友可以參考下
    2015-01-01
  • Android?配合Mat工具監(jiān)聽查找內存泄漏的操作方法

    Android?配合Mat工具監(jiān)聽查找內存泄漏的操作方法

    這篇文章主要介紹了Android?配合Mat工具監(jiān)聽查找內存泄漏問題,使用Android Studio Profiler查看內存的操作,本文通過圖文實例相結合給大家介紹的非常詳細,需要的朋友可以參考下
    2022-05-05
  • android UI進階之android中隱藏的layout 抽屜的使用方法

    android UI進階之android中隱藏的layout 抽屜的使用方法

    android UI進階之android中隱藏的layout 抽屜的使用方法,需要的朋友可以參考一下
    2013-05-05

最新評論