我正在努力理解这个概念,fitsSystemWindows因为它取决于它做不同的事情.根据官方文件,它是一个
布尔内部属性,用于根据系统窗口(如状态栏)调整视图布局.如果为true,则调整此视图的填充以为系统窗口留出空间.
现在,检查View.java类我可以看到,当设置true为时,窗口插入(状态栏,导航栏...)将应用于视图填充,这根据上面引用的文档工作.这是代码的相关部分:
private boolean fitSystemWindowsInt(Rect insets) {
if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
mUserPaddingStart = UNDEFINED_PADDING;
mUserPaddingEnd = UNDEFINED_PADDING;
Rect localInsets = sThreadLocal.get();
if (localInsets == null) {
localInsets = new Rect();
sThreadLocal.set(localInsets);
}
boolean res = computeFitSystemWindows(insets, localInsets);
mUserPaddingLeftInitial = localInsets.left;
mUserPaddingRightInitial = localInsets.right;
internalSetPadding(localInsets.left, localInsets.top,
localInsets.right, localInsets.bottom);
return res;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
随着新材料设计的出现,有一些新的类广泛使用这个标志,这就是混乱的来源.在许多来源中fitsSystemWindows被提到作为设置在系统栏后面放置视图的标志.看到这里.
在文档ViewCompat.java的setFitsSystemWindows说:
设置此视图是否应考虑系统屏幕装饰,例如状态栏和插入其内容; 也就是说,控制是否执行{@link View#fitSystemWindows(Rect)}的默认实现.有关详细信息,请参阅该方法.
据此, …
我SingleFramgnetActivity的目的只是保持和替换其中的碎片.
布局看起来像这样:
<?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:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".SingleFragmentActivity"
>
<include layout="@layout/toolbar"/>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我正在替换FragmentsFrameLayout内部.当我fitsSystemWindows在Fragment布局上将其设置为true时,它没有响应.实际上它只在Activity创建时才起作用,但是一旦我更换了Fragment内部FrameLayout,fitsSystemWindows就会忽略该参数,并且布局位于状态栏和导航栏下方.
我找到了一些自定义FrameLayout的解决方案,它使用了弃用的方法,但由于某种原因它不适用于我(与普通FrameLayout相同的结果),我也不喜欢使用弃用方法的想法.
我的应用程序有一个活动,每个部分都有不同的片段.我最近通过设置fitSystemWindows为状态栏半透明,将true其设置为应用程序的背景颜色.这适用于具有工具栏的片段,颜色匹配,如下所示:

然而,我的一个片段有一张照片和一个半透明的工具栏,所以我想让照片占据状态栏的空间,而不是背景颜色.
我认为,解决的办法是设置fitSystemWindows到false了只有片段,并手动添加填充到半透明的工具栏.以编程方式执行此操作似乎没有任何效果,我可能做错了什么?
这是我的主要活动布局:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">
<!-- Container for various fragment layouts, including nav drawer and tool bar -->
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
从我的片段的onCreateView()中:
RelativeLayout daddyLayout = (RelativeLayout)getActivity().findViewById(R.id.main_parent_view);
daddyLayout.setFitsSystemWindows(false);
daddyLayout.invalidate();
Run Code Online (Sandbox Code Playgroud)
这似乎没有效果,如下:

如果我在中设置fitSystemWindows为false main_parent_view,状态栏填充消失了它可以工作,但显然会影响每个片段.
android android-layout android-fragments android-view android-styles
我正在开发一个我想要启用透明状态栏的应用程序.我想要一个ImageView(我的scaleType属性需要)覆盖整个屏幕,以便图像显示在状态栏下方.这是我的布局:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@drawable/picture" />
<android.support.v7.widget.Toolbar
(...toolbar stuff...)
/>
(...some other stuff...)
</RelativeLayout>
<include layout="@layout/drawer" />
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
如你所见,这是一个常规的DrawerLayout.请注意DrawerLayout,the RelativeLayout和the ImageViewall都将fitsSystemWindows属性设置为true.
我的问题是:如果我设置背景资源(如颜色或图片)来完成的DrawerLayout或RelativeLayout在上面的代码中,我可以看到"下面"的状态栏的颜色或图片,正是我所想要的,但ImageView's'src'drawable总是显示在它下面,好像它fitsSystemWindows完全忽略了属性一样.
我的主题中有以下属性:
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@color/primary_dark</item>
Run Code Online (Sandbox Code Playgroud)
随着primary_dark是一个半透明的黑色(这不应该怎样都无所谓,因为正如我所说,这似乎是专属我的一个问题ImageView.透明状态栏的作品完美无论是DrawerLayout或RelativeLayout.
我试图显示一个带有match_parent的DialogFragment高度和宽度,但它发生在顶部,DialogFragment显示在StatusBar下面.
DialogFragment在底部,右侧,左侧和顶部应用了一些默认值.但顶部填充应从statusBar开始计数,而不是从总屏幕大小开始计算.
如何将DialogFragment设置为match_parent,但是在顶部,底部,右侧,左侧具有正常/默认填充?
android android-dialog android-windowmanager android-dialogfragment android-window
systemWindowInsetBottom 和 stableInsetBottom 都是 0
我有一个 Activity,它有一个背景纹理,我使用 FLAG_LAYOUT_NO_LIMITS 让该背景位于状态和导航栏后面。但我不希望视图的其他内容落后于那些系统 UI 组件。
一开始我想使用resources.getIdentifier("navigation_bar_height", "dimen", "android")来获取导航栏的高度,但这只是导航栏的默认高度,因此它不适用于用户隐藏导航栏的设备。(三星设备)
然后我发现了 WindowInsets 和 android:fitsSystemWindows="true"
它适用于状态栏,但不适用于导航栏。
在我的活动中
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//These 2 flags don't seem to change anything
window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN)
window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR)
//This flag is required so the background can go behind the navbar
window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
rootView.setOnApplyWindowInsetsListener { _, insets ->
val topTextViewParams = rootView.tvTop.layoutParams as ViewGroup.MarginLayoutParams
topTextViewParams.topMargin = insets.systemWindowInsetTop
val bottomTextViewParams = rootView.tvBottom.layoutParams as ViewGroup.MarginLayoutParams
bottomTextViewParams.bottomMargin = insets.systemWindowInsetBottom //Inset is always 0
insets.consumeSystemWindowInsets() …Run Code Online (Sandbox Code Playgroud)