Android PopUpWindow使用详解

Calandra ·
更新时间:2024-09-20
· 1478 次阅读

目录

概述

声明

构造方法

显示函数

正常声明一个PopupWindow代码

设置需要载入的布局

创建PopupWindow

设置显示位置

完整代码

概述

最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)。而PopupWindow是可以指定显示位置的,随便哪个位置都可以,更加灵活。

声明 构造方法 //一: 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应该有三个参数 上下文 宽、高

显示函数 //相对某个控件的位置(正左下方),无偏移 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代码 设置需要载入的布局 <?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> 创建PopupWindow //将xml文件转成view View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.popupwindow,null); //创建popupWindow PopupWindow popupWindow = new PopupWindow(MainActivity.this); //载入布局 popupWindow.setContentView(view); //设置宽高 popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); 设置显示位置 //设置显示位置 正左下方无偏移 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文件转成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); //进行正常页面设置 //设置显示位置 正左下方无偏移 popupWindow.showAsDropDown(mTvShow); //popupwindow 点击外围页面 不会自动消失 因此需要手动设置一个关闭 view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); } }); } }

到此这篇关于Android PopUpWindow使用详解的文章就介绍到这了,更多相关Android PopUpWindow内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



popupwindow Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号