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

聲明
構(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)
一個窗口標準的PopupWindow應該有三個參數(shù) 上下文 寬、高
顯示函數(shù)
//相對某個控件的位置(正左下方),無偏移 showAsDropDown(View anchor): //相對某個控件的位置,有偏移;xoff表示x軸的偏移 showAsDropDown(View anchor, int xoff, int yoff): //正中央Gravity.CENTER,下方Gravity.BOTTOM等 showAtLocation(View parent, int gravity, int x, int y):
這里有兩種顯示方式:
1、顯示在某個指定控件的下方
showAsDropDown(View anchor):
showAsDropDown(View anchor, int xoff, int yoff);
2、指定父視圖,顯示在父控件的某個位置(Gravity.TOP,Gravity.RIGHT等)
showAtLocation(View parent, int gravity, int x, int y);
3、view可以是任意組件
正常聲明一個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="我是一個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);
//進行正常頁面設(shè)置
//設(shè)置顯示位置 正左下方無偏移
popupWindow.showAsDropDown(mTvShow);
//popupwindow 點擊外圍頁面 不會自動消失 因此需要手動設(shè)置一個關(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 RecycleView實現(xiàn)多級樹形列表
這篇文章主要為大家詳細介紹了android RecycleView實現(xiàn)多級樹形列表,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05
Android編程實現(xiàn)仿iphone抖動效果的方法(附源碼)
這篇文章主要介紹了Android編程實現(xiàn)仿iphone抖動效果的方法,結(jié)合實例形式分析了仿iphone抖動效果的頁面布局及功能實現(xiàn)技巧,并附帶實例源碼供讀者下載,需要的朋友可以參考下2015-11-11
Android自定義實現(xiàn)可回彈的ScollView
這篇文章主要為大家詳細介紹了Android自定義實現(xiàn)可回彈的ScollView,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
Android獲取網(wǎng)絡(luò)圖片并顯示的方法
這篇文章主要為大家詳細介紹了Android獲取網(wǎng)絡(luò)圖片并顯示的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11
Android 開發(fā)中l(wèi)ayout下的子文件夾
這篇文章主要介紹了android 開發(fā)中l(wèi)ayout下的子文件夾,需要的朋友可以參考下2017-12-12
android開發(fā)之為activity增加左右手勢識別示例
這篇文章主要介紹了android開發(fā)中為activity增加左右手勢識別示例,需要的朋友可以參考下2014-04-04

