Android widght之PopupWindow
来源:互联网 发布:玩dnf网络冲突怎么办 编辑:程序博客网 时间:2024/06/06 18:13
简介
一个可以用来显示任意视图的弹出窗口。弹出窗口是一个浮动的容器,它出现在当前活动的顶部。
构造方法
public PopupWindow ()
public PopupWindow(Context context)
public PopupWindow (Context context, AttributeSet attrs)
public PopupWindow (Context context, AttributeSet attrs, int defStyleAttr)
public PopupWindow (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
public PopupWindow (View contentView)
contentView(弹出的内容,可以说是你自己 自定义 的 View)public PopupWindow (int width, int height)
width(弹出的宽度)
height(弹出的高度)public PopupWindow (View contentView, int width, int height)
contentView(弹出的内容,可以说是你自己 自定义 的 View)
width(弹出的宽度)
height(弹出的高度)public PopupWindow (View contentView, int width, int height, boolean focusable)
contentView(弹出的内容,可以说是你自己 自定义 的 View)
width(弹出的宽度)
height(弹出的高度)
focusable(如果弹出窗口可以聚焦,则为true,否则为false)
可以看出来:前五个构造方法 与 我们自定义View 差不多(需要重写),我么能使用PopupWindow 时,最常用的就是后面几种,什么意思呢?
- 创建一个新的弹出窗口,它可以显示 你自己 自定义的 一个布局,还可以传入需要显示的宽高,当然到底需要传入这些参数可以根据自己我需求!
公共方法
dismiss ()
隐藏弹出窗口。只有在showAsDropDown(android.view.view)执行之后才能调用此方法。如果不这样做,调用这个方法将没有效果。isShowing()
显示这个弹出窗口是否显示在屏幕上。setContentView(View contentView)
改变弹出的 你自己 自定义的 布局。setHeight(int height)
设置弹出的高度。setWidth(int width)
设置弹出的宽度。setOnDismissListener(PopupWindow.OnDismissListener onDismissListener)
设置在关闭窗口时调用的监听器。setFocusable(boolean focusable)
设置弹出窗口的焦点。setAnimationStyle(int animationStyle)
设置这个弹出窗的动画风格资源。setBackgroundDrawable(Drawable background)
指定这个弹出窗口的背景。showAsDropDown(View anchor)
在 anchor 视图的左下角的一个弹出窗口中显示内容视图。showAsDropDown(View anchor, int xoff, int yoff)
在一个弹出窗口中显示内容视图,该窗口位于 anchor 视图的左下角,被指定的x和y坐标偏移。showAsDropDown(View anchor, int xoff, int yoff, int gravity)
与上一个显示方法相同,但是可以视图的显示位置。showAtLocation(View parent, int gravity, int x, int y)
在指定位置的弹出窗口中显示内容视图。
简单解释 parent 这个参数 (从父视图中获取 getWindowToken() 令牌,即指定PopupWindow 显示在那一个个控件的父视图中)- update(View anchor, int width, int height)
- update(View anchor, int width, int height)
- update(View anchor, int xoff, int yoff, int width, int height)
- update()
- update(int x, int y, int width, int height, boolean force)
- update(int width, int height)
以上六个方法都是刷新PopupWindow 显示位置的!必须在showAsDropDown或者showAtLocation方法后调用,单独调用无任何效果!
例子
主界面布局:
<?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" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tv_cjl" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#bababa" android:gravity="center" android:text="张三" android:textColor="#dc1818" android:textSize="18sp" /> <TextView android:id="@+id/tv_ljp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_weight="1" android:background="#bababa" android:gravity="center" android:text="李四" android:textColor="#dc1818" android:textSize="18sp" /> <TextView android:id="@+id/tv_hjz" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_weight="1" android:background="#bababa" android:gravity="center" android:text="王五" android:textColor="#dc1818" android:textSize="18sp" /> </LinearLayout></LinearLayout>
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" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#cecccc" android:orientation="horizontal" android:padding="12dp"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="姓 名 : " android:textColor="#ff0000" android:textSize="18sp" /> <TextView android:id="@+id/tv_name" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="张三" android:textColor="#a7ade6" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:background="#cecccc" android:orientation="horizontal" android:padding="12dp"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="性 别 : " android:textColor="#ff0000" android:textSize="18sp" /> <TextView android:id="@+id/tv_sex" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="男" android:textColor="#a7ade6" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:background="#cecccc" android:orientation="horizontal" android:padding="12dp"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="年 龄 : " android:textColor="#ff0000" android:textSize="18sp" /> <TextView android:id="@+id/tv_age" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="24" android:textColor="#a7ade6" android:textSize="18sp" /> </LinearLayout></LinearLayout>
测试代码:
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private TextView CJL; private TextView LJP; private TextView HJZ; private PopupWindow pop; private View view; private TextView Name; private TextView Sex; private TextView Age; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initPop(); } private void initPop() { view = LayoutInflater.from(this).inflate(R.layout.pop_detalis, null); view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); Name = ((TextView) view.findViewById(R.id.tv_name)); Sex = ((TextView) view.findViewById(R.id.tv_sex)); Age = ((TextView) view.findViewById(R.id.tv_age)); pop = new PopupWindow(view); pop.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); pop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); pop.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { pop.dismiss(); } }); } private String[][] details = {{"张三", "女", "24"}, {"李四", "男", "24"}, {"王五", "女", "28"}}; private void initView() { CJL = ((TextView) findViewById(R.id.tv_cjl)); LJP = ((TextView) findViewById(R.id.tv_ljp)); HJZ = ((TextView) findViewById(R.id.tv_hjz)); CJL.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showPopupWindow(v, 0); } }); LJP.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showPopupWindow(v, 1); } }); HJZ.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showPopupWindow(v, 2); } }); } private void showPopupWindow(final View v, int index) { if (pop.isShowing()) { pop.dismiss(); } else { Name.setText(details[index][0]); Sex.setText(details[index][1]); Age.setText(details[index][2]); if (index == 2) { pop.showAsDropDown(v, 0, 8); if (view.getWidth() == 0) { new Handler().postDelayed(new Runnable() { @Override public void run() { int offX = v.getWidth() - view.getWidth(); pop.update(v, offX, 8, pop.getWidth(), pop.getHeight()); } }, 100); } else { int offX = v.getWidth() - view.getWidth(); pop.update(v, offX, 8, pop.getWidth(), pop.getHeight()); } } else { pop.showAsDropDown(v, 0, 8); } } }}
效果图:
简单更改一下参数:
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private TextView CJL; private TextView LJP; private TextView HJZ; private PopupWindow pop; private View view; private TextView Name; private TextView Sex; private TextView Age; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initPop(); } private void initPop() { view = LayoutInflater.from(this).inflate(R.layout.pop_detalis, null); view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); Name = ((TextView) view.findViewById(R.id.tv_name)); Sex = ((TextView) view.findViewById(R.id.tv_sex)); Age = ((TextView) view.findViewById(R.id.tv_age)); pop = new PopupWindow(view); pop.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); pop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); pop.setFocusable(true); //pop.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#82000000"))); pop.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { pop.dismiss(); } }); } private String[][] details = {{"张三", "女", "24"}, {"李四", "男", "24"}, {"王五", "女", "28"}}; private void initView() { CJL = ((TextView) findViewById(R.id.tv_cjl)); LJP = ((TextView) findViewById(R.id.tv_ljp)); HJZ = ((TextView) findViewById(R.id.tv_hjz)); CJL.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showPopupWindow(v, 0); } }); LJP.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showPopupWindow(v, 1); } }); HJZ.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showPopupWindow(v, 2); } }); } private void showPopupWindow(final View v, int index) { if (pop.isShowing()) { pop.dismiss(); } else { Name.setText(details[index][0]); Sex.setText(details[index][1]); Age.setText(details[index][2]); if (index == 2) { pop.showAsDropDown(v, 0, 8); if (view.getWidth() == 0) { new Handler().postDelayed(new Runnable() { @Override public void run() { int offX = v.getWidth() - view.getWidth(); pop.update(v, offX, 8, pop.getWidth(), pop.getHeight()); } }, 100); } else { int offX = v.getWidth() - view.getWidth(); pop.update(v, offX, 8, pop.getWidth(), pop.getHeight()); } } else { pop.showAsDropDown(v, 0, 8); } } }}
效果与上面的并没有什么太大区别,但是我调用了
- pop.setFocusable(true);
这句代码之后就发生了大改变,没调用之前 点击手机的返回按键 就会直接无视PopupWindow的显示直接退出当前Activity,而我调之后点击返回按键就会首先执行PopupWindow的dismiss()方法,隐藏PopupWindow,再次点击才会销毁Activity。
知识贵在分享!
- Android widght之PopupWindow
- Android widght之GridView
- Android开发之PopupWindow
- Android之PopupWindow
- android之PopupWindow
- android控件之popupWindow
- Android之动画PopupWindow
- android之PopupWindow
- Android开发之PopupWindow
- android组件之PopupWindow
- android学习之popupWindow
- Android开发之PopupWindow
- Android开发之PopupWindow
- Android之动画PopupWindow
- Android开发之PopupWindow
- Android学习之PopupWindow
- Android总结之PopupWindow
- Android窗口之PopupWindow
- Leetcode-5: Longest Palindromic Substring
- Seal Report使用教程(二)——项目文件
- 不使用第三个值,实现两个变量的互换
- 网络编程(一)--URL
- 数据库视频|自己不懂的点汇总
- Android widght之PopupWindow
- 看懂STL中的sort排序
- 带内管理
- UVA658-It's not a Bug, it's a Feature!-最短路
- C语言入门:查找子串
- 1045. 快速排序
- 南阳oj108--士兵杀敌(一)(线段树,求和)
- 集合框架(集合的继承体系结构)
- python模块以及导入出现ImportError: No module named 'xxx'问题