Android自定义PopupWindow Android自定义PopupWindow简单小例子
mr_zdd 人气:0想了解Android自定义PopupWindow简单小例子的相关内容吗,mr_zdd在本文为您仔细讲解Android自定义PopupWindow的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,PopupWindow,下面大家一起来学习吧。
最近没事做就写了一下PopupWindow,希望对有些人有点帮助。
照常先看一下完成后的结果(界面比较难看就不要吐槽了)
点击地理位置然后弹出的PopupWindow,数据我写死了但是可以根据你们的需求自己改,或者通过网络获取数据。我是通过listView进行展示的你们也可以改成表格布局,具体的实现代码如下:
PopupWindow的弹出框的整体布局(listView)fragment_popup:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/pop_path" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout>
listview要加载的item:pop_list_adapter.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/item_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18dp"/> </LinearLayout>
listview的适配器:PopAdapter
public class PopAdapter extends BaseAdapter { private List<String> list; private Context context; public PopAdapter(List<String> list, Context context) { this.list = list; this.context = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView == null) { viewHolder = new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.pop_list_adapter, null); viewHolder.item_content = (TextView) convertView.findViewById(R.id.item_content); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.item_content.setText(list.get(position)); return convertView; } private class ViewHolder { private TextView item_content; } }
写一个MyPopupWindow类继承PopupWindow:
public class MyPopuWindow extends PopupWindow { private View contentView; private ListView lv_pop; private List<String> paths; private Context context; public MyPopuWindow(final Activity context) { this.context = context; //获得 LayoutInflater 的实例 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); contentView = inflater.inflate(R.layout.fragment_popup, null); //获取屏幕的宽高 int h = context.getWindowManager().getDefaultDisplay().getHeight(); int w = context.getWindowManager().getDefaultDisplay().getWidth(); this.setContentView(contentView); // 设置SelectPicPopupWindow弹出窗体的宽 this.setWidth(LayoutParams.MATCH_PARENT); // 设置SelectPicPopupWindow弹出窗体的高 this.setHeight(LayoutParams.WRAP_CONTENT); // 设置SelectPicPopupWindow弹出窗体可点击 this.setFocusable(true); this.setOutsideTouchable(true); // 刷新状态 this.update(); // 实例化一个ColorDrawable颜色为半透明 ColorDrawable dw = new ColorDrawable(0000000000); // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作 this.setBackgroundDrawable(dw); // 设置SelectPicPopupWindow弹出窗体动画效果 this.setAnimationStyle(R.style.AnimationPreview); initData(); } private void initData() { paths = new ArrayList<>(); paths.add("北京"); paths.add("上海"); paths.add("广州"); paths.add("天津"); paths.add("大连"); paths.add("长春"); paths.add("济南"); paths.add("青岛"); paths.add("无锡"); paths.add("郑州"); paths.add("宁波"); paths.add("厦门"); lv_pop = (ListView) contentView.findViewById(R.id.pop_path); lv_pop.setAdapter(new PopAdapter(paths, context)); lv_pop.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(context, paths.get(position), Toast.LENGTH_SHORT).show(); showPopupWindow(view); } }); } /** * 显示popupWindow * * @param parent */ public void showPopupWindow(View parent) { if (!this.isShowing()) { // 以下拉方式显示popupwindow this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18); } else { this.dismiss(); } } }
接下来就是调用PopupWindow显示了。actionPath:是你的组件也就是我的地理位置
myPopuWindow= new MyPopuWindow(getActivity()); myPopuWindow.showPopupWindow(actionPath);
好了大概的一个代码就是这样了希望对你们有用。
加载全部内容