1.PopupWindow布局文件(Layout)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e6e6e6">
<Button
android:id="@+id/load"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_50"
android:visibility="gone"
style="?android:attr/borderlessButtonStyle"
android:background="@android:color/white"
android:text="查看原图"/>
<include layout="@layout/line_gray_0_3"/>
<Button
android:id="@+id/pic"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_50"
style="?android:attr/borderlessButtonStyle"
android:background="@android:color/white"
android:text="从相册选择图片"/>
<include layout="@layout/line_gray_0_3"/>
<Button
android:id="@+id/tack_photo"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_50"
style="?android:attr/borderlessButtonStyle"
android:background="@android:color/white"
android:text="拍照"/>
<Button
android:id="@+id/cancle"
android:layout_marginTop="@dimen/dp_5"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_50"
android:background="@android:color/white"
android:text="取消"/>
</LinearLayout>
2.PopupWindow逻辑的处理
private View popupView;
private PopupWindow window;
private void showPopupWindow() {
popupView = getLayoutInflater().inflate(R.layout.pop_pic_photo, null);
// 创建PopupWindow对象,指定宽度和高度
window = new PopupWindow(popupView,
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
View viewById = popupView.findViewById(R.id.pic);
View viewById1 = popupView.findViewById(R.id.tack_photo);
View viewById2 = popupView.findViewById(R.id.cancle);
//给内部控件添加点击事件
viewById.setOnClickListener(this);
viewById1.setOnClickListener(this);
viewById2.setOnClickListener(this);
// TODO: 设置动画
// window.setAnimationStyle(R.style.popup_window_anim);
// TODO: 设置背景颜色
window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F8F8F8")));
// TODO: 设置可以获取焦点
window.setFocusable(true);
// TODO: 设置可以触摸弹出框以外的区域
window.setOutsideTouchable(true);
// TODO:更新popupwindow的状态
// window.update();
// TODO: 以下拉的方式显示,并且可以设置显示的位置
window.showAtLocation(showSqs, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
}