替换片段时,Android fitsSystemWindows无法正常工作

替换片段时,Android fitsSystemWindows无法正常工作,第1张

概述我有SingleFramgnetActivity,其目的只是保存和替换其中的片段.布局看起来像这样:<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apkes/android"xmlns:tools="http://schemas.android.como

我有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>

我正在替换FrameLayout中的Fragments.当我在Fragment布局上将fitsSystemwindows设置为true时,它没有响应.实际上它只在创建Activity时才有效,但是一旦我在FrameLayout中替换Fragment,就会忽略fitsSystemwindows参数,并且布局位于状态栏和导航栏下方.

我找到了一些带有自定义FrameLayout的solution,它使用了弃用的方法,但由于某些原因它不适用于我(与普通FrameLayout相同的结果),我也不喜欢使用弃用方法的想法.

解决方法:

你的FrameLayout不知道窗口插入大小,因为它是父级 – linearLayout没有发送任何内容.作为一种变通方法,您可以继承linearLayout子类并将insets传递给子项:

@TargetAPI(Build.VERSION_CODES.KITKAT_WATCH)@OverrIDepublic WindowInsets onApplyWindowInsets(WindowInsets insets) {    int childCount = getChildCount();    for (int index = 0; index < childCount; index++)        getChildAt(index).dispatchApplyWindowInsets(insets); // let children kNow about WindowInsets    return insets;}

您可以查看my this答案,其中将详细说明其工作原理,以及如何使用ViewCompat.setOnApplyWindowInsetsListener API.

总结

以上是内存溢出为你收集整理的替换片段时,Android fitsSystemWindows无法正常工作全部内容,希望文章能够帮你解决替换片段时,Android fitsSystemWindows无法正常工作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1098083.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存