popupwindow优点:可以显示在屏幕中的任何位置
效果图:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent" >
<!--半透明色-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:padding="10dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F6F2F2"
android:text="拍照"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="从相册选取"
android:layout_marginTop="5dp"
android:background="#F6F2F2"/>
<Button
android:layout_marginTop="15dp"
android:id="@+id/btu_cancal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消"
android:background="#F6F2F2"/>
</LinearLayout>
</RelativeLayout>
调用函数:
//显示popupwindow对话框
public void showpopupwindow(){
//创建popupwindow对象
final PopupWindow popupWindow=new PopupWindow(this);
//设置它的视图
View view= getLayoutInflater() .inflate(R.layout.popup_window,null);
//设置视图中的属性和监听器
//设置弹出窗口的宽度
popupWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
Button btucancal=view.findViewById(R.id.btu_cancal);
btucancal.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
popupWindow.setContentView(view);
//显示popupWindow必须显示指定的位置
LinearLayout linearLayout=findViewById(R.id.LinnerLayout);
popupWindow.showAtLocation(linearLayout, Gravity.BOTTOM,0,0);
}