效果图:
item_add.xml
<?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="match_parent"
android:background="#D1D1D1"
android:orientation="vertical" >
<Button
android:id="@+id/album"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="从相册中选取图片"
android:gravity="center"
android:textColor="#595959"
android:textSize="15sp"
android:background="@drawable/kuang_grey_white"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
/>
<Button
android:id="@+id/camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拍照选取图片"
android:gravity="center"
android:textSize="15sp"
android:textColor="#595959"
android:background="@drawable/kuang_grey_white"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
/>
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消"
android:textSize="15sp"
android:gravity="center"
android:textColor="#595959"
android:background="@drawable/kuang_grey_white"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="#D1D1D1"
/>
</LinearLayout>
kuang_grey_white.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="1px" android:color="@color/view" />
<solid android:color="@color/white" />填充色
<corners android:radius="8dp" /> </shape>
方法:
public void showpopupwindow() {
LayoutInflater inflater = LayoutInflater.from(PersonalInfoActivity.this);
View v = inflater.inflate(R.layout.item_add, null);
Button album = (Button) v.findViewById(R.id.album);
Button camera = (Button) v.findViewById(R.id.camera);
Button cancel = (Button) v.findViewById(R.id.cancel);
album.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 相册选择图片
//setalbumImg();
}
});
camera.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
// setcameraImg(type2);
}
});
cancel.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
pw.dismiss();
}
});
// 一般来说,我们在onCreate里面得到的控件的宽高全是0.采用下面的方法,可以得到真实的宽高
int w = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
v.measure(w, h);
int v_h = v.getMeasuredHeight();
int v_w = v.getMeasuredWidth(); pw = new PopupWindow(v, LayoutParams.MATCH_PARENT, v_h);
pw.setFocusable(true);
}
使用:
private PopupWindow pw;
showpopupwindow();
pw.showAtLocation(findViewById(R.id.m), Gravity.BOTTOM, 0, 0);//m是整个当前的根布局id