Android学习之PopupWindow
???? Android的對(duì)話框有兩種:PopupWindow和AlertDialog。 詳細(xì)說(shuō)明如下: AlertDialog是非阻塞式對(duì)話框:AlertDialog彈出時(shí),后臺(tái)還可以做事情; AlertDialog的位置固定,而PopupWindow的位置可以隨意; AlertDialog彈出時(shí),背景是黑色的,但是當(dāng)我們點(diǎn)擊背景,AlertDialog會(huì)消失,證明程序不僅響應(yīng)AlertDialog的操作,還響應(yīng)其他操作,其他程序沒(méi)有被阻塞,這說(shuō)明了AlertDialog是非阻塞式對(duì)話框; PopupWindow是阻塞式對(duì)話框:PopupWindow彈出時(shí),程序會(huì)等待,在PopupWindow退出前,程序一直等待,只有當(dāng)我們調(diào)用了dismiss方法的后,PopupWindow退出,程序才會(huì)向下執(zhí)行。 PopupWindow的位置按照有無(wú)偏移分,可以分為偏移和無(wú)偏移兩種;按照參照物的不同,可以分為相對(duì)于某個(gè)控件(Anchor錨)和相對(duì)于父控件; PopupWindow彈出時(shí),背景沒(méi)有什么變化,但是當(dāng)我們點(diǎn)擊背景的時(shí)候,程序沒(méi)有響應(yīng),只允許我們操作PopupWindow,其他操作被阻塞。
一.使用popupWindow只需2步即可:
1.調(diào)用popupWindow的構(gòu)造器創(chuàng)建PopupWindow對(duì)象;
2.調(diào)用popupWindow的方法將popupWindow顯示出來(lái),方法有三種:
? ??(1)popupWindow.showAtLocation(btn, Gravity.CENTER, 0, 0);
????(2)popupWindow.showAsDropDown(btn);//相對(duì)某個(gè)控件的位置(正左下方),無(wú)偏移
??? ?(3)popupWindow.showAsDropDown(btn, 10, 10);//相對(duì)某個(gè)控件的位置,有偏移
二.其它常用設(shè)置:
?? ?(1)popupWindow.setFocusable(true);//默認(rèn)是false,當(dāng)設(shè)置為true時(shí),系統(tǒng)會(huì)捕獲到焦點(diǎn)給popupWindow; 設(shè)置此參數(shù)獲得焦點(diǎn),否則無(wú)法點(diǎn)擊; ?
??? (2)popupWindow.setOutsideTouchable(true); //點(diǎn)擊PopupWindow區(qū)域外部,PopupWindow消失 ?
????(3)popupWindow.setAnimationStyle(R.style.PopupAnimation); //可以設(shè)置動(dòng)畫(huà) ?
??? (4)new PopupWindow(view,ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);//在構(gòu)造函數(shù)中可以設(shè)置popupwindow的大小;
三. 簡(jiǎn)單實(shí)例:
1.效果圖:
2.XML:
activity_main頁(yè)面:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:gravity="center_horizontal" 6 android:orientation="vertical" > 7 8 <Button 9 android:id="@+id/btn" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="彈出泡泡窗口" /> 13 14 </LinearLayout>popup頁(yè)面:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:gravity="center_horizontal" 6 android:orientation="vertical" > 7 8 <ImageView 9 android:layout_width="240dp" 10 android:layout_height="wrap_content" 11 android:src="@drawable/img" /> 12 13 <Button 14 android:id="@+id/close" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:text="關(guān)閉" /> 18 19 </LinearLayout>3.java代碼:
1 package com.example.popupwindow; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Gravity; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 import android.widget.PopupWindow; 10 11 public class MainActivity extends Activity { 12 13 private Button btn; 14 private Button btnClose; 15 private View inflaterView; 16 17 @Override 18 public void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 initView(); 23 24 } 25 26 private void initView() { 27 // 裝載R.layout.popup對(duì)應(yīng)的界面布局 28 inflaterView = this.getLayoutInflater().inflate(R.layout.popup, null); 29 btnClose = (Button) inflaterView.findViewById(R.id.close); 30 btn = (Button) this.findViewById(R.id.btn); 31 32 final PopupWindow popupWindow = new PopupWindow(inflaterView, 300, 400); 33 btn.setOnClickListener(new OnClickListener() { 34 @Override 35 public void onClick(View arg0) { 36 popupWindow.showAtLocation(btn, Gravity.CENTER, 20, 20); 37 38 } 39 }); 40 41 btnClose.setOnClickListener(new OnClickListener() { 42 @Override 43 public void onClick(View arg0) { 44 popupWindow.dismiss(); // 關(guān)閉窗口 45 } 46 }); 47 } 48 }?
總結(jié)
以上是生活随笔為你收集整理的Android学习之PopupWindow的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: struts中文问题,struts国际化
- 下一篇: Sqlce与SQL Server2000