1,activity的xml布局(布局中有个Button按钮,点击按钮弹出一个popupwindow )
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_launcher_background"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:onClick="click"
android:textSize="30sp"
android:text="弹窗Popupwindow"/>
</LinearLayout>
2,上图中有白色背景的popupwindow 的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:gravity="center"
android:id="@+id/ll_popup">
<LinearLayout
android:id="@+id/ll_popup_content"
android:layout_width="600dp"
android:layout_height="400dp"
android:gravity="center"
android:background="@android:color/white">
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="250dp"
android:padding="10dp"
android:src="@drawable/fengjing"
android:layout_marginRight="20dp"/>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="250dp"
android:padding="10dp"
android:src="@drawable/small"/>
</LinearLayout>
</LinearLayout>
3,MainActivity的代码实现:
public class MainActivity extends AppCompatActivity {
private View pop;
private LinearLayout ll_popup,ll_popup_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pop = View.inflate(this, R.layout.popupwindow_test, null);
ll_popup = pop.findViewById(R.id.ll_popup);
ll_popup_content = pop.findViewById(R.id.ll_popup_content);
ll_popup_content.setOnClickListener(null);//Linearloyout就不可点击了
}
public void click(View view) {
final PopupWindow popupWindow = new PopupWindow(pop, MATCH_PARENT, MATCH_PARENT);
ll_popup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.TOP,0,0);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
//完全沉浸式
super.onWindowFocusChanged(hasFocus);
if (hasFocus && Build.VERSION.SDK_INT >= 19) {
View decorView = getWindow().getDecorView(http://www.my516.com);
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
}
---------------------