PopupWindow使用总结:
看了很多次公司软件中的菜单功能,从5.88开始没有在使用android系统自带的菜单,而是使用了popupWindow代替了系统菜单,其实使用popupWindow作为菜单并不是说它比系统菜单功能强大,只是使用popupWindow更容易控制,用户界面也相对美观了。今天抽时间看了一下,现做一点总结,以供以后使用参考:
这里使用一个Gridview作为菜单中的项:
我们定义一个XML的布局文件:popuwindow_layout.xml
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:gravity="center" android:layout_height="fill_parent"
android:layout_gravity="center" >
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content" android:gravity="center"
android:layout_height="wrap_content" android:layout_gravity="center"
android:background="@drawable/downbutton_corner">
<GridViewandroid:id="@+id/popup_gridview" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:numColumns="4"
android:verticalSpacing="5dip" android:horizontalSpacing="5dip"
android:stretchMode="columnWidth" android:gravity="center"
android:layout_gravity="center" />
LinearLayout>
LinearLayout>
可以看到里面仅仅只有一个GridView组建。
接下来就是定义popupWindow,并将GridView的这个布局文件,作为它的显示样式:
LayoutInflater mLayoutInflater=
(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup menuView=
(ViewGroup)mLayoutInflater.inflate(R.layout.popuwindow_layout.,null,true);//将上面的xml文件转换为GroupView
//定义
PopupWindow popupWindow=new PopupWindow(menuView);
//设置背景为透明
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//添加出现和消失动画
popupWindow.setAnimationStyle(R.style.PopupAnimation);
//这个动画将在后面给出xml
这些设置好后就应该设置popupWindow中View的类容:
首先获取View,这里是一个GridView
GridView gw=(GridView)menuView.findViewById(R.id.popup_gridview);
设置adpter:
gw.setAdapter(adpter);//后面将给出这个adapter的代码
//强制GridView聚焦
gw.requestFocus();
//设置popupWindow的宽高和布局
popupWindow.setWidth(width);//一般为全屏宽度
popupWindow.setHeight(height);
popupWindow.setFocusable(true);
//获取popupWindow将要显示的父容器,一般是一个布局文件的最外层的一个Layout
View parentView=。。。。。
//设置位置及父容器
popupWindow.showAtLocation(parentView,Gravity.CENTER|Gravity.BOTTOM,0,bottom);
popupWindow.update();
这样一个popupWindow就定义好了,其实不只是定义好,这样写已经把它显示出来了,我们只要设置showAtLoocation方法就是显示popupWindow,所以这个方法应该在我们需要显示时才调用。
手动隐藏popupWindow:
if(popupWindow!=null && popupWindow.isShowing()){
popupWindow.dissmiss();
}
//显示隐藏动画xml
<style name="PopupAnimation" parent="android:Animation">
<item name="android:windowEnterAnimation">
@anim/popup_up_initem>
<item name="android:windowExitAnimation">
@anim/popup_down_outitem>
style>
其中:popup_up_in.xml如下:
<set <set
xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:interpolator="@android:anim/decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:pivotY="100%p"
android:duration="300" />
popup_down_out.xml如下:
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:interpolator="@android:anim/decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="1.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotY="100%p"
android:duration="300" />
set>
Adapter:的代码如下:
public class GridViewAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private Integer[] ids;
private String[] names;
private int type;
public GridViewAdapter(Context c, int type, Integer[] ids, String[] names) {
this.type = type;
this.ids = ids;
this.names = names;
mContext = c;
mInflater = LayoutInflater.from(mContext);
}
public void SetData(int type, Integer[] ids, String[] names){
this.type = type;
this.ids = ids;
this.names = names;
}
public int getCount() {
return ids.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public void setIds(Integer[] ids) {
this.ids = ids;
// notifyDataSetChanged();
}
public void setNames(String[] names) {
this.names = names;
notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
float scale = 1;
if((((WindowsManager)mContext) instanceof MainMenuScreen) || (((WindowsManager)mContext) instanceof TradeMenuNew)){
if (type == 0)
scale = Globe.scale_icon * 0.9f;
else if (type == 1)
scale = Globe.scale_icon;
else {
scale=Globe.scale_icon;
}
}
Bitmap img = BaseFuction.createBitmap(mContext.getResources(),
ids[position], scale, scale);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.gridview_ele, null);
holder = new Holder();
holder.icon = (ImageView) convertView
.findViewById(R.id.gridview_ele_img);
holder.text = (TextView) convertView
.findViewById(R.id.gridview_ele_tx);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.icon.setImageBitmap(img);
holder.text.setText(names[position]);
holder.text.setTextSize(14);
return convertView;
}
class Holder {
TextView text;
ImageView icon;
}
}