Android PopUpWindow使用詳解
概述
最關(guān)鍵的區(qū)別是AlertDialog不能指定顯示位置,只能默認(rèn)顯示在屏幕最中間(當(dāng)然也可以通過(guò)設(shè)置WindowManager參數(shù)來(lái)改變位置)。而PopupWindow是可以指定顯示位置的,隨便哪個(gè)位置都可以,更加靈活。

聲明
構(gòu)造方法
//一: public PopupWindow (Context context) //二: public PopupWindow(View contentView) //三: public PopupWindow(View contentView, int width, int height) //四: public PopupWindow(View contentView, int width, int height, boolean focusable)
一個(gè)窗口標(biāo)準(zhǔn)的PopupWindow應(yīng)該有三個(gè)參數(shù) 上下文 寬、高
顯示函數(shù)
//相對(duì)某個(gè)控件的位置(正左下方),無(wú)偏移 showAsDropDown(View anchor): //相對(duì)某個(gè)控件的位置,有偏移;xoff表示x軸的偏移 showAsDropDown(View anchor, int xoff, int yoff): //正中央Gravity.CENTER,下方Gravity.BOTTOM等 showAtLocation(View parent, int gravity, int x, int y):
這里有兩種顯示方式:
1、顯示在某個(gè)指定控件的下方
showAsDropDown(View anchor):
showAsDropDown(View anchor, int xoff, int yoff);
2、指定父視圖,顯示在父控件的某個(gè)位置(Gravity.TOP,Gravity.RIGHT等)
showAtLocation(View parent, int gravity, int x, int y);
3、view可以是任意組件
正常聲明一個(gè)PopupWindow代碼
設(shè)置需要載入的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000"
android:text="我是一個(gè)PopupWindow"
/>
</LinearLayout>
創(chuàng)建PopupWindow
//將xml文件轉(zhuǎn)成view
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.popupwindow,null);
//創(chuàng)建popupWindow
PopupWindow popupWindow = new PopupWindow(MainActivity.this);
//載入布局
popupWindow.setContentView(view);
//設(shè)置寬高
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
設(shè)置顯示位置
//設(shè)置顯示位置 正左下方無(wú)偏移
popupWindow.showAsDropDown(mTvShow);
完整代碼
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView mTvShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTvShow = findViewById(R.id.tvshow);
mTvShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myPopupWindow();
}
});
}
private void myPopupWindow(){
//將xml文件轉(zhuǎn)成view
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.popupwindow,null);
PopupWindow popupWindow = new PopupWindow(MainActivity.this);
popupWindow.setContentView(view);
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
//PopupWindow popupWindow = new PopupWindow(MainActivity.this,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);
//進(jìn)行正常頁(yè)面設(shè)置
//設(shè)置顯示位置 正左下方無(wú)偏移
popupWindow.showAsDropDown(mTvShow);
//popupwindow 點(diǎn)擊外圍頁(yè)面 不會(huì)自動(dòng)消失 因此需要手動(dòng)設(shè)置一個(gè)關(guān)閉
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
}
}

到此這篇關(guān)于Android PopUpWindow使用詳解的文章就介紹到這了,更多相關(guān)Android PopUpWindow內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android RecycleView實(shí)現(xiàn)多級(jí)樹(shù)形列表
這篇文章主要為大家詳細(xì)介紹了android RecycleView實(shí)現(xiàn)多級(jí)樹(shù)形列表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
Android編程實(shí)現(xiàn)屏幕禁止休眠的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)屏幕禁止休眠的方法,分析了Android的Manifest.xml設(shè)置與代碼實(shí)現(xiàn)兩種操作技巧,需要的朋友可以參考下2016-10-10
Android編程實(shí)現(xiàn)仿iphone抖動(dòng)效果的方法(附源碼)
這篇文章主要介紹了Android編程實(shí)現(xiàn)仿iphone抖動(dòng)效果的方法,結(jié)合實(shí)例形式分析了仿iphone抖動(dòng)效果的頁(yè)面布局及功能實(shí)現(xiàn)技巧,并附帶實(shí)例源碼供讀者下載,需要的朋友可以參考下2015-11-11
Android Service啟動(dòng)過(guò)程完整分析
這篇文章主要為大家詳細(xì)分析了Android Service啟動(dòng)完整過(guò)程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
實(shí)例詳解Android解決按鈕重復(fù)點(diǎn)擊問(wèn)題
在項(xiàng)目中,由于網(wǎng)絡(luò)問(wèn)題,不知道這個(gè)按鈕被點(diǎn)擊了幾次,為了防止這一問(wèn)題發(fā)生,下面小編寫(xiě)了一段實(shí)例代碼給大家詳解android解決按鈕重復(fù)點(diǎn)擊問(wèn)題,對(duì)android按鈕重復(fù)點(diǎn)擊相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧2015-12-12
Android自定義實(shí)現(xiàn)可回彈的ScollView
這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)可回彈的ScollView,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android獲取網(wǎng)絡(luò)圖片并顯示的方法
這篇文章主要為大家詳細(xì)介紹了Android獲取網(wǎng)絡(luò)圖片并顯示的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
android相冊(cè)選擇圖片的編碼實(shí)現(xiàn)代碼
本篇文章主要介紹了android相冊(cè)選擇圖片的編碼實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
Android 開(kāi)發(fā)中l(wèi)ayout下的子文件夾
這篇文章主要介紹了android 開(kāi)發(fā)中l(wèi)ayout下的子文件夾,需要的朋友可以參考下2017-12-12
android開(kāi)發(fā)之為activity增加左右手勢(shì)識(shí)別示例
這篇文章主要介紹了android開(kāi)發(fā)中為activity增加左右手勢(shì)識(shí)別示例,需要的朋友可以參考下2014-04-04

