一个简单的自定义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); } }
相关文章推荐
- LINUX
- 理解RESTful架构
- 解读:nginx的一个神秘配置worker_cpu_affinity
- 随手记点-mapreduce5
- Linux笔记(68)——nginx缓存配置及其他配置
- cookie中maxAge总是-1和tomcat设置编码方式
- VMware安装CentOS无法自动获取IP问题
- ubuntu中添加非标准路径的动态库
- linux常用命令
- 在 Linux 上使用开源软件创建 SDN
- 不能不知道的10个angularjs英文学习网站
- 【娱乐】耗叔游戏fun.coolshell.cn 攻略
- Nginx+Tomcat+Session 高性能群集搭建
- Tomcat并发数优化的方法总结
- openCV3学习笔记-开门篇
- 第二章第一节——介绍和软件架构
- 零售企业自建商城网站已是大势所趋
- 使用runloop阻塞线程的正确写法
- Linux 命令收集
- build job和email job的两段shell脚本