Android PopUpWindow使用詳解
概述
最關(guān)鍵的區(qū)別是AlertDialog不能指定顯示位置,只能默認顯示在屏幕最中間(當(dāng)然也可以通過設(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應(yīng)該有三個參數(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 OnCreate()中獲取控件高度與寬度兩種方法詳解
這篇文章主要介紹了Android OnCreate()中獲取控件高度與寬度兩種方法詳解的相關(guān)資料,這里提供了兩種方法,大家可以都看下,需要的朋友可以參考下2016-12-12
Android如何實現(xiàn)URL轉(zhuǎn)換成二維碼
這篇文章主要為大家詳細介紹了Android實現(xiàn)URL轉(zhuǎn)換成二維碼的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04

