替换片段时,Android fitsSystemWindows无法正常工作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了替换片段时,Android fitsSystemWindows无法正常工作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1515字,纯文字阅读大概需要3分钟。
内容图文
![替换片段时,Android fitsSystemWindows无法正常工作](/upload/InfoBanner/zyjiaocheng/951/2b00d645cfa04bfcaa03adce47cf4665.jpg)
我有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)
@Override
public 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无法正常工作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。