Android 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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Android Studio 4.0 正式发布在Ubuntu 20.04中安装的方法
这篇文章主要介绍了Android Studio 4.0 正式发布如何在Ubuntu 20.04中安装,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-06-06Android BadTokenException异常解决案例详解
这篇文章主要介绍了Android BadTokenException异常详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2021-08-08Android 实现不依赖焦点和选中的TextView跑马灯
本文主要介绍Android 跑马灯的实现,这里提供实现详细实现代码供大家参考,有需要的小伙伴可以看下2016-07-07
最新评论