欢迎使用CSDN-markdown编辑器
来源:互联网 发布:淘宝退换货说明 编辑:程序博客网 时间:2024/06/16 07:37
- PopUpWindow使用详解一基本使用
- 一概述
- 1PopupWindow与AlertDialog的区别
- 2PopupWindow的相关函数
- 二简单示例showAtLocation显示窗体
- 1主布局mainxml
- 2PopupWindow布局popuplayoutxml
- 3MainActivity代码
- 1首先看OnCreate
- 2显示PopupWindow
- 第一部分设置ContentView
- 第二部分设置各个控件的点击响应
- 一概述
PopUpWindow使用详解(一)——基本使用
一、概述
1、PopupWindow与AlertDialog的区别
最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)。而PopupWindow是可以指定显示位置的,随便哪个位置都可以,更加灵活。
2、PopupWindow的相关函数
(1)、构造函数:
//方法一: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最基本的三个条件是一定要设置的:View contentView,int width, int height ;少任意一个就不可能弹出来PopupWindow!!!!
所以,如果使用方法一来构造PopupWindow,那完整的构造代码应该是这样的:
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);PopupWindwo popWnd = PopupWindow (context);popWnd.setContentView(contentView);popWnd.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);popWnd.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
new PopupWindow(contentView, mutualDipDpUtil.dip2px(context, 80) , mutualDipDpUtil.dip2px(context,80), true);
有关为什么一定要设置width和height的原因,我们后面会讲,这里说一下为什么样强制设置contentView;很简单的原因是因为PopupWindow没有默认布局,它不会像AlertDialog那样只setTitle,就能弹出来一个框。PopupWindow是没有默认布局的,它的布局只有通过我们自己设置才行。由于方法三中,含有了这三个必备条件,不用单独设置contentview或者width、height,所以构造方法三是用的最多的一个构造方法。
最后,方法四中的focusable变量不是必须的,有关它的方法和意义,我们会在下一篇中细讲。
(2)显示函数
显示函数主要使用下面三个:
//相对某个控件的位置(正左下方),无偏移showAsDropDown(View anchor)://相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上;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)其它函数
public void dismiss()//另外几个函数,这里不讲其意义,下篇细讲public void setFocusable(boolean focusable)public void setTouchable(boolean touchable)public void setOutsideTouchable(boolean touchable)public void setBackgroundDrawable(Drawable background)
二、简单示例(showAtLocation显示窗体)
1、主布局(main.xml)
从效果图中也可以看到主布局只有一个button,什么都没有,所以它的布局代码哪下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="show popWindow"/></LinearLayout>
2、PopupWindow布局(popuplayout.xml)
在概述中,我们提到了,必须为PopupWindow设置布局,从效果图中,我也可以看到它的布局有三个item,中间用横线分开。所以这里布局使用Listview应该更合适,但为了减轻代码难度,我们直接使用TextView和分隔线来代替,
代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="vertical" android:paddingBottom="2dp"> <View android:layout_width="match_parent" android:layout_height="2.25dp" android:background="#fa7829" android:layout_alignParentTop="true"/> <TextView android:id="@+id/pop_computer" android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/pop_text_style" android:text="计算机"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@drawable/list_line"/> <TextView android:id="@+id/pop_financial" android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/pop_text_style" android:text="金融"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@drawable/list_line"/> <TextView android:id="@+id/pop_manage" android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/pop_text_style" android:text="管理"/> <View android:layout_width="match_parent" android:layout_height="1dp"/></LinearLayout>
3、MainActivity代码
先贴出来完整代码,然后再逐步讲解:
public class MainActivity extends Activity implements View.OnClickListener{ private PopupWindow mPopWindow; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showPopupWindow(); } }); } private void showPopupWindow() { //设置contentView View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null); mPopWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true); mPopWindow.setContentView(contentView); //设置各个控件的点击响应 TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer); TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial); TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage); tv1.setOnClickListener(this); tv2.setOnClickListener(this); tv3.setOnClickListener(this); //显示PopupWindow View rootview = LayoutInflater.from(MainActivity.this).inflate(R.layout.main, null); mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0); } @Override public void onClick(View v) { int id = v.getId(); switch (id){ case R.id.pop_computer:{ Toast.makeText(this,"clicked computer",Toast.LENGTH_SHORT).show(); mPopWindow.dismiss(); } break; case R.id.pop_financial:{ Toast.makeText(this,"clicked financial",Toast.LENGTH_SHORT).show(); mPopWindow.dismiss(); } break; case R.id.pop_manage:{ Toast.makeText(this,"clicked manage",Toast.LENGTH_SHORT).show(); mPopWindow.dismiss(); } break; } }}
(1)首先看OnCreate()
在OnCreate()中只做了一个操作,当点击Button时,显示窗体:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showPopupWindow(); } });}
(2)、显示PopupWindow
下面是有关窗体操作的代码:
private void showPopupWindow() { //设置contentView View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null); mPopWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true); //设置各个控件的点击响应 TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer); TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial); TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage); tv1.setOnClickListener(this); tv2.setOnClickListener(this); tv3.setOnClickListener(this); //显示PopupWindow View rootview = LayoutInflater.from(MainActivity.this).inflate(R.layout.main, null); mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);}
这里同样分为三部分:
第一部分:设置ContentView
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);mPopWindow = new PopupWindow(contentView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
利用LayoutInflater获取R.layout.popuplayout对应的View,然后利用我们上面所讲的构造函数三来生成mPopWindow;
这里 要注意一个问题:在这个构造函数里,我们传进去了width和height全部都是WRAP_CONTENT;而在R.layout.popuplayou的根布局中,我们定义的width和height代码是:layout_width=”fill_parent”,layout_height=”wrap_content”;原代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="vertical" android:paddingBottom="2dp"> ………………</LinearLayout>
这里就有冲突了,那显示出来的popupWindow是按哪个来的呢?
从效果图中来看,明显PopupWindow宽度并没有全屏,显然是按代码中的布局为准。
这说明了:
如果在代码中重新设置了popupWindow的宽和高,那就以代码中所设置为准。(至于原因,下篇会讲)
第二部分:设置各个控件的点击响应
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- highchart的几种数据组织形式
- 十大Intellij IDEA快捷键
- Java集合框架详解(二)——Collection源码分析
- fedora25 26系统 SecureCRT SecureFX 注册破解方法
- Sigma Function (找规律?)
- 欢迎使用CSDN-markdown编辑器
- PermGen space错误解决方法
- Visual Box 的 Host-only 连接
- 前端mvc Model和Control 要分别两个.js文件写吗?
- 着陆之后的路人甲
- hadoop伪集群配置
- python读文件read()、readline()、readlines()三个方法的区别
- hdu3068(最长回文串)
- AndroidStudio + MAT 内存泄漏分析