日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android学习之PopupWindow

發(fā)布時(shí)間:2023/12/10 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android学习之PopupWindow 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

???? Android的對話框有兩種:PopupWindow和AlertDialog。 詳細(xì)說明如下: AlertDialog是非阻塞式對話框:AlertDialog彈出時(shí),后臺(tái)還可以做事情; AlertDialog的位置固定,而PopupWindow的位置可以隨意; AlertDialog彈出時(shí),背景是黑色的,但是當(dāng)我們點(diǎn)擊背景,AlertDialog會(huì)消失,證明程序不僅響應(yīng)AlertDialog的操作,還響應(yīng)其他操作,其他程序沒有被阻塞,這說明了AlertDialog是非阻塞式對話框; PopupWindow是阻塞式對話框:PopupWindow彈出時(shí),程序會(huì)等待,在PopupWindow退出前,程序一直等待,只有當(dāng)我們調(diào)用了dismiss方法的后,PopupWindow退出,程序才會(huì)向下執(zhí)行。 PopupWindow的位置按照有無偏移分,可以分為偏移和無偏移兩種;按照參照物的不同,可以分為相對于某個(gè)控件(Anchor錨)和相對于父控件; PopupWindow彈出時(shí),背景沒有什么變化,但是當(dāng)我們點(diǎn)擊背景的時(shí)候,程序沒有響應(yīng),只允許我們操作PopupWindow,其他操作被阻塞。

一.使用popupWindow只需2步即可:

1.調(diào)用popupWindow的構(gòu)造器創(chuàng)建PopupWindow對象;

2.調(diào)用popupWindow的方法將popupWindow顯示出來,方法有三種:

? ??(1)popupWindow.showAtLocation(btn, Gravity.CENTER, 0, 0);

????(2)popupWindow.showAsDropDown(btn);//相對某個(gè)控件的位置(正左下方),無偏移

??? ?(3)popupWindow.showAsDropDown(btn, 10, 10);//相對某個(gè)控件的位置,有偏移

二.其它常用設(shè)置:

?? ?(1)popupWindow.setFocusable(true);//默認(rèn)是false,當(dāng)設(shè)置為true時(shí),系統(tǒng)會(huì)捕獲到焦點(diǎn)給popupWindow; 設(shè)置此參數(shù)獲得焦點(diǎn),否則無法點(diǎn)擊; ?

??? (2)popupWindow.setOutsideTouchable(true); //點(diǎn)擊PopupWindow區(qū)域外部,PopupWindow消失 ?

????(3)popupWindow.setAnimationStyle(R.style.PopupAnimation); //可以設(shè)置動(dòng)畫 ?

??? (4)new PopupWindow(view,ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);//在構(gòu)造函數(shù)中可以設(shè)置popupwindow的大小;

三. 簡單實(shí)例:

1.效果圖:

2.XML:

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>

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 <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對應(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的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。