PopupWindow使用总结:

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

这些设置好后就应该设置popupWindowView的类容:
首先获取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.BOTTOM0bottom);

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_in</item>

<item   name="android:windowExitAnimation">

@anim/popup_down_out</item>

</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" />

</set>

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)mContextinstanceof MainMenuScreen) || (((WindowsManager)mContextinstanceof 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_elenull);

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;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PopupWindow是一种可以在当前界面上方显示的弹出窗口,通常用于显示一些额外的信息或者提供用户操作的选项。在Android中,可以使用PopupWindow类来创建弹出窗口。 以下是使用PopupWindow的一般步骤: 1. 创建PopupWindow对象:使用PopupWindow的构造函数创建一个PopupWindow对象。 2. 设置PopupWindow的属性:设置PopupWindow的大小、位置、背景等属性。 3. 设置PopupWindow的内容视图:使用setContentView方法设置PopupWindow的内容视图,这可以是一个布局文件或者一个View对象。 4. 显示PopupWindow使用showAsDropDown、showAtLocation等方法显示PopupWindow。 5. 处理PopupWindow的事件:设置PopupWindow的监听器,处理PopupWindow的各种事件。 以下是一个简单的例子,展示如何使用PopupWindow: ``` // 创建PopupWindow对象 PopupWindow popupWindow = new PopupWindow(context); // 设置PopupWindow的属性 popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setFocusable(true); // 设置PopupWindow的内容视图 View contentView = LayoutInflater.from(context).inflate(R.layout.popup_layout, null); popupWindow.setContentView(contentView); // 显示PopupWindow popupWindow.showAsDropDown(anchorView); // 处理PopupWindow的事件 contentView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理点击事件 popupWindow.dismiss(); } }); ``` 在上面的代码中,我们创建了一个PopupWindow对象,并设置了宽高、背景等属性。然后,我们使用LayoutInflater加载了一个布局文件作为PopupWindow的内容视图,并使用setContentView方法设置了PopupWindow的内容视图。最后,我们使用showAsDropDown方法显示了PopupWindow,并设置了一个点击事件处理器来处理用户点击PopupWindow的事件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值