PopupWindow显示在某个控件上方

一、自定义PopupWindow

public class PopupWindowView extends PopupWindow{
    private int popupWidth;
    private int popupHeight;
    private ListView listView;
    private View parentView;
    public PopupWindowView(Context context , ArrayList<PopBean> popBeans ) {
        super(context);
        initView(context);
        setPopConfig();
        initData(context,popBeans);
    }
    /**
     *   初始化控件
     * <h3>Version</h3> 1.0
     * <h3>CreateTime</h3> 2016/6/29,22:00
     * <h3>UpdateTime</h3> 2016/6/29,22:00
     * <h3>CreateAuthor</h3> vera
     * <h3>UpdateAuthor</h3>
     * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
     * @param context
     */
    private void initView(Context context) {
        parentView = View.inflate(context, R.layout.layout_popup_window, null);
        listView = (ListView) parentView.findViewById(R.id.popup_window_list);
        setContentView(parentView);
    }

    /**
     *   初始化数据
     * <h3>Version</h3> 1.0
     * <h3>CreateTime</h3> 2016/6/29,22:03
     * <h3>UpdateTime</h3> 2016/6/29,22:03
     * <h3>CreateAuthor</h3> vera
     * <h3>UpdateAuthor</h3>
     * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
     * @param context
     * @param popBeans
     */
    private void initData(Context context, ArrayList<PopBean> popBeans) {
        //ListView添加数据
        PopListViewAdapter popListViewAdapter = new PopListViewAdapter(context,popBeans);
        listView.setAdapter(popListViewAdapter);
    }
    /**
     *
     * 配置弹出框属性
     * @version 1.0
     *
     * @createTime 2015/12/1,12:45
     * @updateTime 2015/12/1,12:45
     * @createAuthor
     * @updateAuthor
     * @updateInfo (此处输入修改内容,若无修改可不写.)
     *
     */
    private void setPopConfig() {
//        this.setContentView(mDataView);//设置要显示的视图
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置弹出窗体可点击
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0xcc000000);
        this.setBackgroundDrawable(dw);
        this.setOutsideTouchable(true);// 设置外部触摸会关闭窗口

        //获取自身的长宽高
        parentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        popupHeight = parentView.getMeasuredHeight();
        popupWidth = parentView.getMeasuredWidth();
    }
    public ListView getListView() {
        return listView;
    }

    /**
     * 设置显示在v上方(v的左边距为开始位置)
     * @param v
     */
    public void showUp(View v) {
        //获取需要在其上方显示的控件的位置信息
        int[] location = new int[2];
        v.getLocationOnScreen(location);
        //在控件上方显示
        showAtLocation(v, Gravity.NO_GRAVITY, (location[0]) - popupWidth / 2, location[1] - popupHeight);
    }

    /**
     * 设置显示在v上方(以v的中心位置为开始位置)
     * @param v
     */
    public void showUp2(View v) {
        //获取需要在其上方显示的控件的位置信息
        int[] location = new int[2];
        v.getLocationOnScreen(location);
        //在控件上方显示
        showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight);
    }
}
二、布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/popup_window_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>

</LinearLayout>



阅读更多
版权声明:请尊重劳动成果,转载请注明来处:Abang的博客 http://blog.csdn.net/lu1024188315,如果您有不同意见,请尽情留言 https://blog.csdn.net/lu1024188315/article/details/51786656
个人分类: Android UI
上一篇Android调用系统下载器DownloadManager
下一篇Android 断点续传
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭