最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

本文實(shí)例講述了Android編程自定義對(duì)話框(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可以設(shè)置為一個(gè)View也可以簡(jiǎn)單地指定資源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");
    /*
     * 獲取圣誕框的窗口對(duì)象及參數(shù)對(duì)象以修改對(duì)話框的布局設(shè)置,
     * 可以直接調(diào)用getWindow(),表示獲得這個(gè)Activity的Window
     * 對(duì)象,這樣這可以以同樣的方式改變這個(gè)Activity的屬性.
     */
    Window dialogWindow = dialog.getWindow();
    WindowManager.LayoutParams lp = dialogWindow.getAttributes();
    dialogWindow.setGravity(Gravity.LEFT | Gravity.TOP);
    /*
     * lp.x與lp.y表示相對(duì)于原始位置的偏移.
     * 當(dāng)參數(shù)值包含Gravity.LEFT時(shí),對(duì)話框出現(xiàn)在左邊,所以lp.x就表示相對(duì)左邊的偏移,負(fù)值忽略.
     * 當(dāng)參數(shù)值包含Gravity.RIGHT時(shí),對(duì)話框出現(xiàn)在右邊,所以lp.x就表示相對(duì)右邊的偏移,負(fù)值忽略.
     * 當(dāng)參數(shù)值包含Gravity.TOP時(shí),對(duì)話框出現(xiàn)在上邊,所以lp.y就表示相對(duì)上邊的偏移,負(fù)值忽略.
     * 當(dāng)參數(shù)值包含Gravity.BOTTOM時(shí),對(duì)話框出現(xiàn)在下邊,所以lp.y就表示相對(duì)下邊的偏移,負(fù)值忽略.
     * 當(dāng)參數(shù)值包含Gravity.CENTER_HORIZONTAL時(shí)
     * ,對(duì)話框水平居中,所以lp.x就表示在水平居中的位置移動(dòng)lp.x像素,正值向右移動(dòng),負(fù)值向左移動(dòng).
     * 當(dāng)參數(shù)值包含Gravity.CENTER_VERTICAL時(shí)
     * ,對(duì)話框垂直居中,所以lp.y就表示在垂直居中的位置移動(dòng)lp.y像素,正值向右移動(dòng),負(fù)值向左移動(dòng).
     * gravity的默認(rèn)值為Gravity.CENTER,即Gravity.CENTER_HORIZONTAL |
     * Gravity.CENTER_VERTICAL.
     *
     * 本來(lái)setGravity的參數(shù)值為Gravity.LEFT | Gravity.TOP時(shí)對(duì)話框應(yīng)出現(xiàn)在程序的左上角,但在
     * 我手機(jī)上測(cè)試時(shí)發(fā)現(xiàn)距左邊與上邊都有一小段距離,而且垂直坐標(biāo)把程序標(biāo)題欄也計(jì)算在內(nèi)了,
     * Gravity.LEFT, Gravity.TOP, Gravity.BOTTOM與Gravity.RIGHT都是如此,據(jù)邊界有一小段距離
     */
    lp.x = 100; // 新位置X坐標(biāo)
    lp.y = 100; // 新位置Y坐標(biāo)
    lp.width = 300; // 寬度
    lp.height = 300; // 高度
    lp.alpha = 0.7f; // 透明度
    // 當(dāng)Window的Attributes改變時(shí)系統(tǒng)會(huì)調(diào)用此函數(shù),可以直接調(diào)用以應(yīng)用上面對(duì)窗口參數(shù)的更改,也可以用setAttributes
    // dialog.onWindowAttributesChanged(lp);
    dialogWindow.setAttributes(lp);
    /*
     * 將對(duì)話框的大小按屏幕大小的百分比設(shè)置
     */
//    WindowManager m = getWindowManager();
//    Display d = m.getDefaultDisplay(); // 獲取屏幕寬、高用
//    WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 獲取對(duì)話框當(dāng)前的參數(shù)值
//    p.height = (int) (d.getHeight() * 0.6); // 高度設(shè)置為屏幕的0.6
//    p.width = (int) (d.getWidth() * 0.65); // 寬度設(shè)置為屏幕的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>

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

永川市| 土默特右旗| 电白县| 江北区| 奇台县| 仁寿县| 洱源县| 贵阳市| 彭泽县| 二手房| 宁国市| 彭州市| 东安县| 合川市| 长泰县| 同仁县| 陆川县| 庄河市| 漳平市| 禹州市| 舒城县| 彭阳县| 绥芬河市| 青浦区| 通山县| 宽甸| 平潭县| 普格县| 临漳县| 新巴尔虎左旗| 墨竹工卡县| 阜宁县| 正镶白旗| 鱼台县| 颍上县| 五寨县| 法库县| 荆州市| 普定县| 五台县| 莱西市|