上面为弹出来的效果
popUpWindow布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:orientation="vertical"> <LinearLayout android:padding="2dp" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:background="@drawable/popwindow_bg"//使用shape画出圆角矩形,边框为为灰色,背景为白色。颜色不要再线性布局中设置,也会掩盖圆角矩形。 android:orientation="vertical"> <TextView android:id="@+id/takePhoto" style="@style/bottom_pop_textview"//宽不能设为match_parent,否则外层的圆角矩形会被遮盖。 android:text="拍照" /> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray" /> <TextView android:id="@+id/openPhotos" style="@style/bottom_pop_textview" android:layout_marginTop="1dp" android:text="打开相册" /> </LinearLayout> <LinearLayout android:padding="2dp" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:layout_marginTop="10dp" android:background="@drawable/popwindow_bg"//使用shape画出圆角矩形,边框为为灰色,背景为白色。 android:orientation="vertical"> <TextView android:id="@+id/cancel" style="@style/bottom_pop_textview" android:text="取消" /> </LinearLayout> </LinearLayout>
shape画出圆角矩形 代码:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <stroke android:width="1dip" android:color="#ffbbbbbb"/> <corners android:topLeftRadius="15dip" android:topRightRadius="15dip" android:bottomLeftRadius="15dip" android:bottomRightRadius="15dip" /> <solid android:color="@android:color/white"/> </shape>
从底部弹出popupwindow:
/** * 从底部弹出popupwindow */ private void showBottomPop(View parent) { final View popView = View.inflate(getActivity(), R.layout.bottom_pop_layout, null); showAnimation(popView);//开启动画 PopupWindow mPopWindow = new PopupWindow(popView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); clickPopItem(popView, mPopWindow);//条目的点击 mPopWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); mPopWindow.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); mPopWindow.setOutsideTouchable(true); mPopWindow.setFocusable(true); mPopWindow.update(); // 设置背景颜色变暗 WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); lp.alpha = 0.7f; getActivity().getWindow().setAttributes(lp); mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); lp.alpha = 1f; getActivity().getWindow().setAttributes(lp); } }); }
弹出的动画:
/** * 给popupwindow添加动画 * * @param popView */ private void showAnimation(View popView) { AnimationSet animationSet = new AnimationSet(false); AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1.0f); alphaAnimation.setDuration(300); TranslateAnimation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0f ); translateAnimation.setDuration(300); animationSet.addAnimation(alphaAnimation); animationSet.addAnimation(translateAnimation); popView.startAnimation(animationSet); }