用PopupWindow实现下拉列表
2015-09-21 23:09
253 查看
用 PopupWindow(View contentView, int width, int height),添加所需要的View,然后设置popupWindow,再让它显示在某个控件下方
布局
代码:
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#10000000" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:orientation="horizontal" > <TextView android:id="@+id/tv_content" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:layout_weight="1" android:background="@color/white" android:padding="6dp" android:textColor="#cc000000" android:textSize="17sp" /> <ImageButton android:id="@+id/btn_popup_spinner" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginRight="10dp" android:layout_gravity="center_vertical" android:background="@drawable/down_arrow" /> </LinearLayout> </LinearLayout>
代码:
public class PopupSpinnerActivity extends Activity { private TextView tv_content; private PopupWindow popupMenu; private ListView mListView; //下拉列表的ListView private ArrayList<String> contentListData; private MyPopupDataAdapter adapter; private ImageButton btn_popupmenu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_popup_spinner); initPopupSpinnerView(); } /** * 初始化控件 */ private void initPopupSpinnerView() { tv_content = (TextView) findViewById(R.id.tv_content); btn_popupmenu = (ImageButton) findViewById(R.id.btn_popup_spinner); // tv_content.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//加下划线 // tv_content.getPaint().setAntiAlias(true); //抗锯齿 btn_popupmenu.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showSelectPoupWindow(); } }); } /** * 设置下拉列表及内容 */ private void showSelectPoupWindow() { initPopupListData(); popupMenu = new PopupWindow(mListView, tv_content.getWidth() + btn_popupmenu.getWidth(), 200); popupMenu.setOutsideTouchable(true);//设置外部可被点击 popupMenu.setFocusable(true); popupMenu.setBackgroundDrawable(new BitmapDrawable()); popupMenu.showAsDropDown(tv_content, 0, 0); //对齐此控件,后两个参数是距离控件的左和上下距离 } private void initPopupListData() { mListView = new ListView(this); mListView.setDividerHeight(1); mListView.setVerticalScrollBarEnabled(false); //消除滚动条 //mListView.setBackgroundResource(R.drawable.popup_listview_background); contentListData = new ArrayList<String>(); for (int i = 0; i < 20; i++) { String content = "popup list item data " + i; contentListData.add(content); } adapter = new MyPopupDataAdapter(); mListView.setAdapter(adapter); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), "点击了item " + position, Toast.LENGTH_SHORT).show(); String itemContent = contentListData.get(position); tv_content.setText(itemContent); popupMenu.dismiss(); } }); } public class MyPopupDataAdapter extends BaseAdapter { @Override public int getCount() { return contentListData == null ? 0 : contentListData.size(); } @Override public Object getItem(int position) { return contentListData.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = View.inflate(PopupSpinnerActivity.this, R.layout.popup_list_item, null); holder = new ViewHolder(); holder.tv_value = (TextView) convertView.findViewById(R.id.tv_value); holder.btn_delete = (ImageButton) convertView.findViewById(R.id.btn_delete); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.tv_value.setText(contentListData.get(position)); holder.btn_delete.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { contentListData.remove(position); adapter.notifyDataSetChanged(); if (contentListData.size() == 0) { popupMenu.dismiss(); } } }); return convertView; } class ViewHolder { TextView tv_value; ImageButton btn_delete; } } }
相关文章推荐
- Linux命令(3):sz与rz命令
- docker技术网址
- Linux系统--Linux进程与作业管理(2)
- 运维
- JMX监控 Tomcat Glassfish JBoss WebLogic WebSphere
- 新手安装ecshop集合版出现cls_image::gd_version()和不支持JPEG
- linux压缩和解压
- 监控供电电源接法
- Linux下配置Mysql允许远程访问
- doget和dopost的区别
- 多线程-NSOperation
- dockerfile 创建 ssh 镜像
- Hadoop 笔记之shuffle详解过程
- echo命令
- 单行bash、shell、perl命令
- Linux:Vim
- linux下scp使用
- 第 十 一 天 : 本 地 yum 和 源 码 包 举 例
- Win8+CentOS7 双系统安装恢复win8引导
- wmware和virtualbox的区别。