Android PopUpWindow使用詳解
概述
最關(guān)鍵的區(qū)別是AlertDialog不能指定顯示位置,只能默認(rèn)顯示在屏幕最中間(當(dāng)然也可以通過設(shè)置WindowManager參數(shù)來改變位置)。而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ù)
//相對某個(gè)控件的位置(正左下方),無偏移 showAsDropDown(View anchor): //相對某個(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è)置顯示位置 正左下方無偏移
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)行正常頁面設(shè)置
//設(shè)置顯示位置 正左下方無偏移
popupWindow.showAsDropDown(mTvShow);
//popupwindow 點(diǎn)擊外圍頁面 不會(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android實(shí)現(xiàn)橡皮筋回彈和平移縮放效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)橡皮筋回彈和平移縮放效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android OnCreate()中獲取控件高度與寬度兩種方法詳解
這篇文章主要介紹了Android OnCreate()中獲取控件高度與寬度兩種方法詳解的相關(guān)資料,這里提供了兩種方法,大家可以都看下,需要的朋友可以參考下2016-12-12
如何獲取Android設(shè)備掛載的所有存儲(chǔ)器
這篇文章主要為大家詳細(xì)介紹了如何獲取Android設(shè)備掛載的所有存儲(chǔ)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Android如何實(shí)現(xiàn)URL轉(zhuǎn)換成二維碼
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)URL轉(zhuǎn)換成二維碼的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Android邊框裁切的正確姿勢實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Android邊框裁切的正確姿勢實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
簡單實(shí)用的Android UI微博動(dòng)態(tài)點(diǎn)贊效果
這篇文章主要為大家詳細(xì)介紹了簡單實(shí)用的Android UI微博動(dòng)態(tài)點(diǎn)贊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android編程之控件狀態(tài)配置文件實(shí)例
這篇文章主要介紹了Android編程之控件狀態(tài)配置文件,以實(shí)例形式分析了Android控件狀態(tài)配置文件對于選中、獲得焦點(diǎn)、按下時(shí)的狀態(tài)等相關(guān)設(shè)置技巧,需要的朋友可以參考下2016-01-01

