您的位置:首页 > 运维架构

一个简单的自定义popupwindow

2016-03-23 00:31 435 查看
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: