popupwindow的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/Modify_Head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="从相册修改头像"
/>
<View
android:layout_width="@dimen/dp_100"
android:layout_height="@dimen/dp_3"
android:background="#ccc"
/>
<TextView
android:id="@+id/Photograph"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="照相修改头像"
/>
设置popupwindow
//加载popupWindow的子布局
View view = View.inflate(this, R.layout.profileactivity_head_popupwindow, null);
//获取popupWindow中的控件
//通过子布局中的到ID
mModify = (TextView) view.findViewById(R.id.Modify_Head);
mPhotograph = (TextView) view.findViewById(R.id.Photograph);
//1.创建popupwindow contentView 子布局 width,宽 height 高
mPopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//设置焦点
mPopupWindow.setFocusable(true);
//设置背景 ColorDrawable(int) 颜色背景
//1.new ColorDrawable(R.color.bai) 在values下创建color.xml
//2.new ColorDrawable(Color.WHITE)
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.RED));
//设置是否可以触摸
mPopupWindow.setTouchable(true);
}
/**
* 点击头像,弹出popupwindow
*/
@OnClick(R.id.Profile_head_Image)
public void headImage(){
//设置popupwindow出现在mProfileHeadImage控件的下方
mPopupWindow.showAsDropDown(mProfileHeadImage);
//2.根据X Y轴规定位置s
// popupWindow.showAsDropDown(v, 100, 100);
//3.根据父窗体设置 X Y 轴规定位置 parent 父窗体 gravity 重力方向
//popupWindow.showAtLocation(
//View.inflate(MainActivity.this, R.layout.activity_main, null),
// Gravity.CENTER, 100, 100);
}
//4.显示在底部
View rootview = LayoutInflater.from(DetailsActivity.this).inflate(R.layout.activity_details, null);
popupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);
}