popuWindow初识

来源:互联网 发布:淘宝买药怎么审核 编辑:程序博客网 时间:2024/06/06 05:26

感觉写的这个popuwindow挺没用的,但是既然是初次识别,就这样吧
这里写图片描述
先上效果图
这里写图片描述


popuWindow的xml布局

<?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="match_parent">    <TextView        android:text="请选择爱好"        android:layout_height="wrap_content"        android:layout_width="match_parent"        />    <CheckBox        android:id="@+id/chbOne"        android:text="音乐"        android:layout_height="wrap_content"        android:layout_width="match_parent"        />    <CheckBox        android:id="@+id/chbTwo"        android:text="炉石"        android:layout_height="wrap_content"        android:layout_width="match_parent"        />    <CheckBox        android:id="@+id/chbThree"        android:text="羽毛球"        android:layout_height="wrap_content"        android:layout_width="match_parent"        />    <CheckBox        android:id="@+id/chbFour"        android:text="蹦极"        android:layout_height="wrap_content"        android:layout_width="match_parent"        />    <Button        android:id="@+id/btnClick"        android:text="确定"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:layout_gravity="center_horizontal"        /></LinearLayout>

这里写图片描述

主页面上只有一个按钮

<?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_popu_test"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.axnet.duihuakuangtest.PopuTestActivity">    <Button        android:text="Button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true"        android:id="@+id/bt1"/></RelativeLayout>

代码

public class PopuTestActivity extends AppCompatActivity {    private Button bt1;    PopupWindow popupWindow;    LayoutInflater inflater;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_popu_test);        initView();        bt1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //创建popuWindow窗口                initpopupWindow();                if(popupWindow.isShowing()){//判断窗口是否打开                    popupWindow.dismiss();                }else {                    //设置popuWindow显示位置                    popupWindow.showAsDropDown(v);                }            }        });    }    private void initView() {        bt1=(Button)findViewById(R.id.bt1);    }    private void initpopupWindow() {        //创建一个popuwindow窗口 0*0        if(popupWindow==null){            popupWindow=new PopupWindow(PopuTestActivity.this);            //设置显示的窗口布局            inflater=getLayoutInflater();            View view=inflater.inflate(R.layout.layout_popuwindow,null);            //监听popuWindow布局中的点击事件            Button btnClick=(Button)view.findViewById(R.id.btnClick);            btnClick.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    //处理业务逻辑                }            });            popupWindow.setContentView(view);            //  高和宽            popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);            popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);            //设置背景颜色            Drawable drawable=getResources().getDrawable(R.mipmap.ffc0cb);            popupWindow.setBackgroundDrawable(drawable);        }    }}

划重点
声明popupWindow

popupWindow=new PopupWindow(PopuTestActivity.this);

把布局引入popupWindow

  //设置显示的窗口布局 inflater=getLayoutInflater();  View view=  inflater.inflate(R.layout.layout_popuwindow,null);

设置popuwindow的样式

popupWindow.setContentView(view);            //  高和宽            popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);            popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);//设置背景颜色Drawable drawable=getResources().getDrawable(R.mipmap.ffc0cb);            popupWindow.setBackgroundDrawable(drawable);

处理popuWindow中的事件

 //监听popuWindow布局中的点击事件            Button btnClick=(Button)view.findViewById(R.id.btnClick);            btnClick.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    //处理业务逻辑                }            });

判断popuWindow是否被创建
没有创建创建,创建了popuwindow消失

 if(popupWindow.isShowing()){//判断窗口是否打开                    popupWindow.dismiss();                }else {                    //设置popuWindow显示位置                    popupWindow.showAsDropDown(v);                }