標籤:android style blog http io color ar os 使用
Android的對話方塊有兩種:PopupWindow和AlertDialog。
詳細說明如下:
AlertDialog是非阻塞式對話方塊:AlertDialog彈出時,後台還可以做事情;
AlertDialog的位置固定,而PopupWindow的位置可以隨意;
AlertDialog彈出時,背景是黑色的,但是當我們點擊背景,AlertDialog會消失,證明程式不僅響應AlertDialog的操作,還響應其他動作,其他程式沒有被阻塞,這說明了AlertDialog是非阻塞式對話方塊;
PopupWindow是阻塞式對話方塊:PopupWindow彈出時,程式會等待,在PopupWindow退出前,程式一直等待,只有當我們調用了dismiss方法的後,PopupWindow退出,程式才會向下執行。
PopupWindow的位置按照有無位移分,可以分為位移和無位移兩種;按照參照物的不同,可以分為相對於某個控制項(Anchor錨)和相對於父控制項;
PopupWindow彈出時,背景沒有什麼變化,但是當我們點擊背景的時候,程式沒有響應,只允許我們操作PopupWindow,其他動作被阻塞。
一.總結:
PopupWindow的基本用法,如下:
1、為PopupWindow的view布局,通過LayoutInflator擷取布局的view.如:
1 LayoutInflaterinflater=(LayoutInflater)this.anchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2 ViewtextEntry View=inflater.inflate(R.layout.paopao_alert_dialog,null);
2、顯示位置,可以有很多方式設定顯示方式
1 pop.showAtLocation(findViewById(R.id.ll2),Gravity.LEFT,0,-90);
3、進出場動畫
1 pop.setAnimationStyle(R.style.PopupAnimation);
4、點擊PopupWindow地區外部,PopupWindow消失
1 this.window=newPopupWindow(anchor.getContext()); 2 3 this.window.setTouchInterceptor(newOnTouchListener(){ 4 5 @Override 6 7 publicbooleanonTouch(Viewv,MotionEventevent){ 8 9 if(event.getAction()==MotionEvent.ACTION_OUTSIDE){10 11 BetterPopupWindow.this.window.dismiss();12 13 return true;14 15 }16 17 return false;18 19 }20 21 });
1、PopuWindow的大小由下面代碼控制;
newPopupWindow(view,ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
2、popuWindow.showAsDropDown(v);方法是將PopuWindow顯示在Viewv的左下方;
3、需要順利讓PopUpWindowdimiss(即點擊PopuWindow之外的地方此或者back鍵PopuWindow會消失);PopUpWindow的背景不可為空。必須在 popuWindow.showAsDropDown(v);或者其它的顯示PopuWindow方法之前設定它的背景不為空白:如下面兩行代碼:
ColorDrawablecd=newColorDrawable(-0000);
popuWindow.setBackgroundDrawable(cd);
popuWindow.showAsDropDown(v);
注意這裡設定背景並不會覆蓋xml檔案定義的背景。
4、當有popuWindow.setFocusable(false);的時候,說明PopuWindow不能獲得焦點,即使設定設定了背景不為空白也不能點擊外面消失,只能由dismiss()消失,但是外面的View的事件還是可以觸發,back鍵也可以順利dismiss掉。當設定為popuWindow.setFocusable(true);的時候,加上下面兩行設定背景代碼,點擊外面和Back鍵才會消失。
5、//這裡設定顯示PopuWindow之後在外面點擊是否有效。如果為false的話,那麼點擊PopuWindow外面並不會關閉PopuWindow。當然這裡很明顯只能在Touchable下才能使用。
popuWindow.setOutsideTouchable(true);
二.執行個體代碼:
XML:
1.activity_main頁面:
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>
2.popup頁面:
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 <Button14 android:id="@+id/close"15 android:layout_width="wrap_content"16 android:layout_height="wrap_content"17 android:text="關閉" />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 @Override18 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對應的介面布局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 @Override35 public void onClick(View arg0) {36 popupWindow.showAtLocation(btn, Gravity.CENTER, 20, 20);37 38 }39 });40 41 btnClose.setOnClickListener(new OnClickListener() {42 @Override43 public void onClick(View arg0) {44 popupWindow.dismiss(); // 關閉視窗45 }46 });47 }48 }
Android學習之-----PopupWindow