android popupWindow的运用

来源:互联网 发布:淘宝网地摊用衣架 编辑:程序博客网 时间:2024/05/16 14:56

PopupWindow是android中用户比较喜欢的一个控件,运用起来也比较简单,新浪微博客户端中就用到PopupWindow来选择好友分类。

 

今天我们做的就是它了,popupWindow里放一个ListView。

首先我们看到popupWindow里是一个listView,我们用布局实现这个ListView:

<?xml version="1.0"encoding="UTF-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"    android:id = "@+id/popupLayout"   android:layout_width="fill_parent"   android:layout_height="fill_parent"    android:orientation="vertical"    android:background="#aa000000">    <ListView     android:id="@+id/customer_popuplist"     android:layout_width="fill_parent"     android:layout_height="fill_parent"/></LinearLayout>


然后我们就将他实例化并添加到PopupWindow里:

        button =(Button)findViewById(R.id.button);        button.setOnClickListener(newView.OnClickListener() {                                             @Override                       publicvoid onClick(View v) {                               //TODO Auto-generated method stub                               showPopup();                       }               });    }    private void showPopup(){               //listview的显示条目内容        friends= new String[]{"全部","我的微博","周边","未命名","未命名非公有","hhv"};               //得到popupWindow的显示布局        Viewview =LayoutInflater.from(PopupWindowTestActivity.this).inflate(R.layout.popup,null);            LinearLayout popuplayout =(LinearLayout)view.findViewById(R.id.popupLayout);               //实例化listview并设置适配器            ListView list =(ListView)view.findViewById(R.id.customer_popuplist);            list.setAdapter(newArrayAdapter<String>(PopupWindowTestActivity.this,android.R.layout.simple_list_item_1,friends));            //实例化并设置popup属性            popup = newPopupWindow(popuplayout,180,220);//popup的布局及长宽               //强行获得popupWindow里面的内容的焦点(ListView),如果设为false,listView将得不到焦点,点击listview也没有反应            popup.setFocusable(true);               //PopupWindow显示在button下面            popup.showAsDropDown(button,70,0);//第二个参数是离button左边100px,0是离button底部0px            list.setOnItemClickListener(newOnItemClickListener() {                        @Override                       publicvoid onItemClick(AdapterView<?> parent, View v,                                      intposition, long id) {                               button.setText(friends[position]);                               popup.dismiss();                               popup= null;                       }               });    }


 

好了 让我们看一下效果图

 

popupWindow的基本实现就这样了,若要做的更美观,就得在布局上下些功夫了! !  呵呵,就说到这里吧,再见!!

 

原创粉丝点击