android 底部弹出选项,背景透明,PopupWindow的使用
2015-04-30 11:45
411 查看
</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中显示的位置
相关文章推荐
- android 底部弹出菜单(带透明背景)
- Android 底部弹出提示框的解决办法(使用Activity以及PopupWindow)
- 仿IOS底部选项效果--底部弹出PopupWindow,背景变为半透明效果
- android 底部弹出菜单(带透明背景)
- Android-实现底部弹出PopupWindow并让背景逐渐变暗
- Android实现底部弹出PopupWindow背景逐渐变暗效果
- Android实现底部弹出PopupWindow背景逐渐变暗效果
- Android弹出popupWindow 背景变暗(背景半透明)(两种写法)
- Android中popupwindow弹出灰色背景遇到的坑
- android中popupwindow弹出后,屏幕背景变成半透明
- Android 对话框弹出底部背景透明度调节
- Android 弹出PopupWindows背景变透明
- 转 android 从底部弹出一个popuwindow,渐入渐出效果。我这里是用在购物车需要选择购买选项的操作。
- Android中使用PopupWindow 仿微信点赞和评论弹出
- Android底部弹出的popupWindow
- 使用android.graphics.Path类自绘制PopupWindow背景
- Android使用Activity实现从底部弹出菜单或窗口的方法
- iOS实现底部弹出PopupWindow效果 iOS改变背景透明效果
- Android Popupwindow弹出窗口的简单使用方法
- 底部弹出PopupWindow并且背景变为半透明效果