fragment代码:
public void onCreate(Bundle savedInstanceState) {
// 判断Android>5.0才能使用
if (Build.VERSION.SDK_INT >= 21) {
// 好的当前活动的DecorView,在改变UI显示
View decorView = getActivity().getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
// 使其状态栏呈现透明色
getActivity().getWindow().setStatusBarColor(Color.TRANSPARENT);
}
super.onCreate(savedInstanceState);
}
XML代码:
<FrameLayout 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:fitsSystemWindows="true"
tools:context="xjj.com.luomusic.MainFragment">
<pl.droidsonroids.gif.GifImageView
android:id="@+id/music_gif"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@drawable/g02">
</pl.droidsonroids.gif.GifImageView>
<ImageButton
android:id="@+id/imgBt"
android:layout_width="100dp"
android:layout_height="100dp"
android:animateLayoutChanges="true"
android:visibility="invisible"
android:layout_gravity="center"
android:src="@drawable/pause_normal"/>
<include layout="@layout/bottom"></include>
</FrameLayout>
但最终并未透明而是白色:
解决方法为删除其所有根布局的 android:fitsSystemWindows="true" 即可。
参照: