一个简单的自定义popupwindow

来源:互联网 发布:csol网络连接中断 编辑:程序博客网 时间:2024/04/26 17:22

xml布局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/tv_cancel"        android:layout_width="fill_parent"        android:layout_height="40dp"        android:textSize="18sp"        android:text="取消"        android:gravity="center"        android:layout_margin="10dp"        android:layout_alignParentBottom="true"        android:background="@drawable/rect_gray"        android:textColor="@color/gender_color"        />    <LinearLayout        android:layout_above="@id/tv_cancel"        android:layout_width="fill_parent"        android:layout_marginLeft="10dp"        android:layout_marginRight="10dp"        android:layout_height="120dp"        android:id="@+id/pop_layout"        android:background="@drawable/rect_gray"        android:orientation="vertical">        <TextView            android:id="@+id/tv_pop_title"            android:layout_width="fill_parent"            android:layout_height="0dp"            android:text="请选择性别"            android:gravity="center"            android:textColor="@color/textc"            android:layout_weight="1"/>        <View            android:layout_width="fill_parent"            android:background="@color/line"            android:layout_height="1dp"/>        <TextView            android:id="@+id/tv_male"            android:layout_width="fill_parent"            android:layout_height="0dp"            android:text="♂"            android:textColor="@color/gender_color"            android:textSize="18sp"            android:gravity="center"            android:layout_weight="1"/>        <View            android:layout_width="fill_parent"            android:background="@color/line"            android:layout_height="1dp"/>        <TextView            android:layout_width="fill_parent"            android:layout_height="0dp"            android:text="♀"            android:id="@+id/tv_female"            android:textSize="18sp"            android:gravity="center"            android:textColor="@color/gender_color"            android:layout_weight="1"/>    </LinearLayout></RelativeLayout>

public class GenderPopupWindow extends PopupWindow{    private  TextView mCancel;    private  TextView mFemale;    private  TextView mMale;    private  TextView mTitle;    View mMenView;    public GenderPopupWindow(Activity context, View.OnClickListener itemOnClick){        super(context);        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        mMenView = inflater.inflate(R.layout.gender_popup,null);        mMale = (TextView) mMenView.findViewById(R.id.tv_male);        mFemale = (TextView) mMenView.findViewById(R.id.tv_female);        mCancel = (TextView) mMenView.findViewById(R.id.tv_cancel);        mTitle = (TextView)mMenView.findViewById(R.id.tv_pop_title);        mCancel.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                dismiss();            }        });        mMale.setOnClickListener(itemOnClick);        mFemale.setOnClickListener(itemOnClick);        this.setContentView(mMenView);        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);        this.setFocusable(true);//        this.setAnimationStyle(R.style.Anim);        ColorDrawable dw = new ColorDrawable(0xb0000000);        this.setBackgroundDrawable(dw);        mMenView.setOnTouchListener(new View.OnTouchListener() {            public boolean onTouch(View v, MotionEvent event) {                int height = mMenView.findViewById(R.id.pop_layout).getTop();                int y = (int) event.getY();                if (event.getAction() == MotionEvent.ACTION_UP) {                    if (y < height) {                        dismiss();                    }                }                return true;            }        });    }    /**     * 设置名字     * @param name     */    public void setFemaleName(String name) {        mFemale.setText(name);    }    public void setMaleName(String name) {        mMale.setText(name);    }    public void setTitleName(String name) {        mTitle.setText(name);    }}


0 0