Android開發之對話方塊進階應用程式

來源:互聯網
上載者:User

Android開發之對話方塊進階應用程式

Android開發之對話方塊進階應用程式

建立並顯示一個對話方塊很簡單,但是如果想進行一些更進階點的操作,就需要一些技巧了。下面將和大家分享一下對話方塊使用的一些進階技巧。


1.改變對話方塊的顯示位置:

大多說情況下,對話方塊是在螢幕的中間位置顯示的(根據系統版本的不同可能有所差異)。實際上,也可以根據需要在螢幕的左、中、右、上、下,甚至任意位置來顯示對話方塊。

要想擷取對話方塊的顯示的位置,需要獲得對話方塊的Window對象,並通過Window對象的一些方法來控制對話方塊的顯示位置。

例如:

AlertDialog alertDialog=new AlertDialog.Builder(this).setMessage("在螢幕頂端顯示對話方塊").setPositiveButton("確定", null).create();Window window=alertDialog.getWindow();/*調用setGravity方法使對話方塊在螢幕頂端顯示。*/window.setGravity(Gravity.TOP);alertDialog.show();

提示:setGravity方法的參數也支援的如下幾個值:

Gravity.LEFT(左側),Gravity.RIGHT(右側),Gravity.BOTTOM(底部),Gravity.LEFT|Gravity.TOP(左上方)等。

運行:



2.在任意位置顯示對話方塊:

上面已經擷取了對話方塊的Window對象,我們可以通過該對象的getAttributes()方法來擷取WindowManager.LayoutParams對象,並通過WindowManager.LayoutParams對象的WindowManager.LayoutParams.x與WindowManager.LayoutParams.y屬性來設定對話方塊在螢幕中的位置。

AlertDialog alertDialog=new AlertDialog.Builder(this).setMessage("在任意位置顯示對話方塊").setPositiveButton("確定", null).create();Window window=alertDialog.getWindow();                    WindowManager.LayoutParams wl=window.getAttributes();//x,y為對話方塊相對於螢幕的中心位置的位移量//設定水平位移量wl.x=-50;//設定垂直位移量wl.y=100;window.setAttributes(wl);alertDialog.show();

提示:通過WindowManager.LayoutParams.x與WindowManager.LayoutParams.y設定的是對話方塊相對於螢幕中間位置的位移量,並不是螢幕的絕對位置。

運行:



3在對話方塊的內容文本和按鈕中插入圖片度:

我曾在Android開發之TextView進階應用程式中介紹過在TextView中插入映像,這種技術在對話方塊中也同樣適用。在對話方塊中插入圖片的代碼如下:

/** * 在對話方塊的內容文本和按鈕中插入圖片度 * */protected void insertImg() {         // TODO Auto-generated method stub         AlertDialog alertDialog=new AlertDialog.Builder(this)         .setMessage(getHtml()).setPositiveButton(getHtml(), null).create();                   alertDialog.show();}//返回一個Spanned對象private Spanned getHtml() {         // TODO Auto-generated method stub         Spanned spanned=Html.fromHtml("哈哈,你好.", new ImageGetter() {                   @Override                   public Drawable getDrawable(String source) {                            // TODO Auto-generated method stub                            Drawable drawable=getResources().getDrawable(R.drawable.ic_launcher);                            //設定圖片的縮放大小                            drawable.setBounds(0,0,30,30);                            return drawable;                   }         }, null);         return spanned;}

運行:



4.改變對話方塊的透明度

通過WindowManager.LayoutParams.alpha可以設定對話方塊的透明度。Alpha的取值範圍在0.0f至1.0f之間(f表示float類型的數字)。0.0f表示完全透明(這時對話方塊就看不見了),1.0f表示完全不透明。1.0f也是alpha的預設值。下面的代碼顯示了一個Alpha值為0.6f的個對話方塊。

/** * 改變對話方塊的透明度 * */protected void setAlpha() {         // TODO Auto-generated method stub         AlertDialog alertDialog=new AlertDialog.Builder(this)         .setMessage("這是一個透明度為60%的對話方塊!").setPositiveButton("確定", null).create();         Window window=alertDialog.getWindow();         window.setGravity(Gravity.TOP);         WindowManager.LayoutParams wl=window.getAttributes();         //alpha的範圍是0.0f-1.0f         wl.alpha=0.6f;         window.setAttributes(wl);         alertDialog.show();}

運行:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.