今天在做软件的时候用到了PopUpWindow,于是在网上搜集了一下关于PopUpWindows相关的知识记录一下。
首先是效果:
这个效果很容易理解:当点击btn时,在底部弹出PopupWindow,然后点击各个item弹出对应toast。
<pre name="code" class="java">//方法一: 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)
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);有关为什么一定要设置width和height的原因,我们后面会讲,这里说一下为什么样强制设置contentView;很简单的原因是因为PopupWindow没有默认布局,它不会像AlertDialog那样只setTitle,就能弹出来一个框。PopupWindow是没有默认布局的,它的布局只有通过我们自己设置才行。由于方法三中,含有了这三个必备条件,不用单独设置contentview或者width、height,所以构造方法三是用的最多的一个构造方法。
显示函数主要使用下面三个:
//相对某个控件的位置(正左下方),无偏移 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):
public void dismiss() //另外几个函数,这里不讲其意义,下篇细讲 public void setFocusable(boolean focusable) public void setTouchable(boolean touchable) public void setOutsideTouchable(boolean touchable) public void setBackgroundDrawable(Drawable background)
从效果图中也可以看到主布局只有一个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>
<?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>
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; } } }
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); //设置各个控件的点击响应 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); }
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null); mPopWindow = new PopupWindow(contentView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
<?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>
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);
TextView tv1 = (TextView)findViewById(R.id.pop_computer);
@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; } }
View rootview = LayoutInflater.from(MainActivity.this).inflate(R.layout.main, null); mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);
到这里,有关PopupWindow的显示及其中控件响应基本都讲完了,下面,我们就讲讲showAsDropDown显示窗体的用法。
<?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"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffff"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:textColor="#50484b" android:padding="10dp" android:text="返回"/> <TextView android:id="@+id/menu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:textColor="#50484b" android:padding="10dp" android:text="菜单"/> </RelativeLayout> </LinearLayout>
<?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="@drawable/pop_bg" android:orientation="vertical" android:paddingBottom="2dp"> <TextView android:id="@+id/pop_computer" android:layout_width="wrap_content" 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="wrap_content" 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="wrap_content" android:layout_height="wrap_content" style="@style/pop_text_style" android:text="管理"/> <View android:layout_width="match_parent" android:layout_height="1dp"/> </LinearLayout>
public class MainActivity extends Activity implements View.OnClickListener{ private PopupWindow mPopWindow; private TextView mMenuTv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mMenuTv = (TextView)findViewById(R.id.menu); mMenuTv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showPopupWindow(); } }); } private void showPopupWindow() { View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null); mPopWindow = new PopupWindow(contentView); mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); 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); mPopWindow.showAsDropDown(mMenuTv); } @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; } } }
private void showPopupWindow() { View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null); mPopWindow = new PopupWindow(contentView); mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); 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); mPopWindow.showAsDropDown(mMenuTv); }
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null); mPopWindow = new PopupWindow(contentView); mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
mPopWindow.showAsDropDown(mMenuTv);
从效果图中可以看出,这部分是上一个示例的升级版,就是在点出PopupWindow的同时,把背景用半透明黑色遮罩住,像弹出AlertDialog一样的效果。下面就来看看这个效果是怎么实现的吧。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#66000000"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/pop_bg" android:orientation="vertical" android:paddingBottom="2dp" android:layout_alignParentRight="true"> <TextView android:id="@+id/pop_computer" android:layout_width="wrap_content" 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="wrap_content" 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="wrap_content" android:layout_height="wrap_content" style="@style/pop_text_style" android:text="管理"/> <View android:layout_width="match_parent" android:layout_height="1dp"/> </LinearLayout> </RelativeLayout>
private void showPopupWindow() { View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null); mPopWindow = new PopupWindow(contentView); mPopWindow.setWidth(ViewGroup.LayoutParams.FILL_PARENT); mPopWindow.setHeight(ViewGroup.LayoutParams.FILL_PARENT); ………………//设置各控件点击响应 mPopWindow.showAsDropDown(mMenuTv); }
先看看效果:
为PopupWindow添加动画并不难,只需要使用一个函数即可:
//设置动画所对应的style mPopWindow.setAnimationStyle(R.style.contextMenuAnim);
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="@android:integer/config_shortAnimTime" android:fromXDelta="0" android:fromYDelta="100%p" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:toXDelta="0" android:toYDelta="0"/> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="@android:integer/config_shortAnimTime" android:fromXDelta="0" android:fromYDelta="0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:toXDelta="0" android:toYDelta="100%p" /> </set>
<style name="contextMenuAnim" parent="@android:style/Animation.Activity"> <item name="android:windowEnterAnimation">@anim/context_menu_enter</item> <item name="android:windowExitAnimation">@anim/context_menu_exit</item> </style>
private void showPopupWindow() { View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null); mPopWindow = new PopupWindow(contentView); mPopWindow.setWidth(ViewGroup.LayoutParams.FILL_PARENT); mPopWindow.setHeight(ViewGroup.LayoutParams.FILL_PARENT); ………………//设置各子项点击响应 mPopWindow.setAnimationStyle(R.style.contextMenuAnim); mPopWindow.showAsDropDown(mMenuTv); }
到这里,这个示例的代码就讲完了。源码在文章底部一起给出。这篇讲述了有关PopupWindow的基本使用方法,下篇将针对还未讲述的几个函数,以及问题点结合源码深入剖析。
源码下载地址:http://download.csdn.net/detail/harvic880925/9195255
PopUpWindow使用详解(二)——进阶及答疑