android 底部弹出选项,背景透明,PopupWindow的使用

来源:互联网 发布:网络广告位招商 编辑:程序博客网 时间:2024/05/01 03:21

</pre>http://www.cnblogs.com/zhwl/p/3332636.html<p></p><p>看了这个文章后我才学会的这个东西,先看下面成品,需求不同就不必浪费时间看下去了</p><p></p><p></p><p>这是需要弹出来的布局文件:</p><p>需要让它覆盖整个屏幕,后面会将背景设为半透明</p><p></p><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <RelativeLayout        android:id="@+id/rl_personal_center_select_sex_man"        android:layout_width="match_parent"        android:layout_height="120dp"        android:layout_alignParentBottom="true"        >        <Button            android:id="@+id/bt_personal_center_select_sex_man"            android:layout_width="match_parent"            android:layout_height="60dp"            android:background="@color/white"            android:text="男"            android:textSize="20sp"            />        <View            android:id="@+id/V_1"            android:layout_width="match_parent"            android:layout_height="1dp"            android:background="#d7d7d7"            android:layout_below="@+id/bt_personal_center_select_sex_man"            />        <Button            android:id="@+id/bt_personal_center_select_sex_woman"            android:layout_width="match_parent"            android:layout_height="60dp"            android:text="女"            android:textSize="20sp"            android:background="@color/white"            android:layout_below="@+id/V_1"            />        </RelativeLayout></RelativeLayout>


这个是PopupWindow类,在需要弹出的activity里面实例化它,然后操作:

import android.content.Context;import android.graphics.drawable.ColorDrawable;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.PopupWindow;import android.widget.RelativeLayout;import com.zuxia.R;import butterknife.ButterKnife;import butterknife.InjectView;/** * Created by Chen 2015/4/29. */public class PopupSelectSexWindow extends PopupWindow{    private View mMenuView;    Button bt_man;    Button bt_woman;    public PopupSelectSexWindow(Context context,View.OnClickListener itemsOnClick){        super(context);        LayoutInflater inflater = (LayoutInflater) context                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);        mMenuView = inflater.inflate(R.layout.popup_select_sex, null);        bt_man = (Button)mMenuView.findViewById(R.id.bt_personal_center_select_sex_man);        bt_woman = (Button)mMenuView.findViewById(R.id.bt_personal_center_select_sex_woman);        //取消按钮//        exit.setOnClickListener(new OnClickListener() {////            public void onClick(View v) {//                //销毁弹出框//                dismiss();//            }//        });//        设置按钮监听        bt_man.setOnClickListener(itemsOnClick);        bt_woman.setOnClickListener(itemsOnClick);        //设置SelectPicPopupWindow的View        this.setContentView(mMenuView);        //设置SelectPicPopupWindow弹出窗体的宽        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);        //设置SelectPicPopupWindow弹出窗体的高        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);        //设置SelectPicPopupWindow弹出窗体可点击        this.setFocusable(true);        //设置SelectPicPopupWindow弹出窗体动画效果        this.setAnimationStyle(R.style.AppTheme);        //实例化一个ColorDrawable颜色为半透明        ColorDrawable dw = new ColorDrawable(0xb0000000);        //设置SelectPicPopupWindow弹出窗体的背景        this.setBackgroundDrawable(dw);        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框        mMenuView.setOnTouchListener(new View.OnTouchListener() {            public boolean onTouch(View v, MotionEvent event) {                int height = mMenuView.findViewById(R.id.rl_personal_center_select_sex).getTop();                int y=(int) event.getY();                if(event.getAction()==MotionEvent.ACTION_UP){                    if(y<height){                        dismiss();                    }                }                return true;            }        });    }    @Override    public void showAtLocation(View parent, int gravity, int x, int y) {        super.showAtLocation(mMenuView, gravity, x, y);    }}

然后在需要弹出popup的activity调用这个,我是把它设置在button的onClick里面,点击性别选择后弹出选项:

 menuWindow = new PopupSelectSexWindow(PersonalInformationAct.this, itemsOnClick);                //显示窗口                menuWindow.showAtLocation(PersonalInformationAct.this.findViewById(R.id.rl_main),                        Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置




0 0
原创粉丝点击