android中的PopupWindow的使用

来源:互联网 发布:爱知产业大学 推荐 编辑:程序博客网 时间:2024/06/05 07:24

PopupWindow有点类似于Dialog,相同点在于都是弹出窗口,并且都可以对其进行自定义显
示,并且里面的监听组件,进行相应的操作,但它与Dialog又有很大的区别,PopupWindow
只是弹出窗口,不会使宿主Activity组件失去焦点,也就是说PopupWindow弹出后,你仍可
以与宿主Activity进行交互,Dialog却不能做到这一点

PopupWindow有几种构造方法,无参数的我们一般不使用,常用的为

public PopupWindow(Context context); //给定一个上下文,当我们设置了setOutsideTouchable为true时,在触摸到弹框外部后自动关闭。但是这个构造方法必须去指定显示的布局,布局的宽高,焦点。 public PopupWindow(View contentView, int width, int height, booleanfocusable) //给定一个布局。宽,高和焦点,但是在了setOutsideTouchable为true时,触摸到弹框外部也不能自动关闭,必须去指定背景,一般使用window.setBackgroundDrawable(new ColorDrawable(0x00000000))

常用方法

setOutsideTouchable(boolean);//指定设置显示PopuWindow之后在外面点击是否有效。 setFocusable(boolean);//指定是否获取焦点 setBackgroundDrawable(new ColorDrawable(0));//没有上下文时必须指定,否者setOutsideTouchable无效 showAsDropDown(anchor, 0, 0);//设置显示PopupWindow的位置位于View的左下方,x,y表示坐标偏移量 showAtLocation(findViewById(R.id.parent), Gravity.LEFT, 0, -90); //(以某个View为参考),表示弹出窗口以parent组件为参考,位于左侧,偏移-90。 setOnDismissListenerd(new PopupWindow.OnDismissListener(){})//设置窗口消失事件

下面一个示例效果图如下:

这里写图片描述

public class MainActivity extends AppCompatActivity implements View.OnClickListener {    List<String> numbers = new ArrayList<>();    ImageView iv_down;    EditText et_content;    RelativeLayout rl;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        for (int i = 0; i < 50; i++) {            numbers.add("13815474" + i);        }        iv_down = (ImageView) findViewById(R.id.iv_down);        et_content = (EditText) findViewById(R.id.et_content);        rl = (RelativeLayout) findViewById(R.id.rlS);        iv_down.setOnClickListener(this);    }    PopupWindow window;    @Override    public void onClick(View v) {        ListView lv = new ListView(this);        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                et_content.setText(numbers.get(position));                window.dismiss();            }        });        lv.setAdapter(adapter);        window = new PopupWindow(lv, rl.getWidth(), 200, true);        window.setOutsideTouchable(true);        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));        window.showAsDropDown(rl, 0, 50);    }    private BaseAdapter adapter = new BaseAdapter() {        @Override        public int getCount() {            return numbers.size();        }        @Override        public Object getItem(int position) {            return null;        }        @Override        public long getItemId(int position) {            return 0;        }        @Override        public View getView(final int position, View convertView, ViewGroup parent) {            ViewHodler holder;            if (convertView == null) {                convertView = View.inflate(getBaseContext(), R.layout.iten_layout, null);                holder = new ViewHodler(convertView);                convertView.setTag(holder);            } else                holder = (ViewHodler) convertView.getTag();            holder.tv_phone.setText(numbers.get(position));            //给一个删除数据的事件            holder.iv_delete.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    numbers.remove(position);                    adapter.notifyDataSetChanged();                }            });//            holder.tv_phone.setOnClickListener(new View.OnClickListener() {//                @Override//                public void onClick(View v) {//                    et_content.setText(numbers.get(position));//                    window.dismiss();//                }//            });            return convertView;        }        class ViewHodler {            TextView tv_phone;            ImageView iv_delete;            public ViewHodler(View convertView) {                tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);                iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);            }        }    };}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.administrator.popupwindowdemo.MainActivity">    <RelativeLayout        android:id="@+id/rlS"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:layout_margin="50dp"        android:background="@drawable/back"        android:padding="5dp">        <EditText            android:id="@+id/et_content"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_margin="10dp"            android:background="@null"            android:hint="请输入电话号码" />        <ImageView            android:id="@+id/iv_down"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:layout_centerVertical="true"            android:src="@mipmap/down_arrow" />    </RelativeLayout></RelativeLayout>

iten_layout.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"    android:gravity="center"    android:orientation="horizontal">    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="5dp"        android:src="@mipmap/user" />    <TextView        android:id="@+id/tv_phone"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:gravity="center"        android:text="电话" />    <ImageView        android:id="@+id/iv_delete"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="5dp"        android:src="@mipmap/delete" /></LinearLayout>
1 0
原创粉丝点击