码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • FrameLayout系列:FrameLayout三连问


    布局专栏里面一直缺一篇FrameLayout的文章。

    目录

    • FrameLayout在哪里用到了?
        • 1. DecorView
        • 2. 动态加载Fragmemt
        • 3. Activity.java的源码中,setContentView()的父布局
    • 官网对于FrameLayout的定位是什么?
    • 什么时候使用FrameLayout?

    FrameLayout在哪里用到了?

    1. DecorView

    DecorView的完整路径,是com.android.internal.policy.DecorView。Android studio中双击shift搜索,能找到DecorView的源代码,如下图:

    从源码可以看到,DecorView extends FrameLayout:

    Activity中,View树的根布局就是DecorView。如下图,DecorView内部分了三次,最底层是一个LinearLayout,它上面覆盖了一个id为navigationBarBackground的View,就是底部导航栏。最上面是一个id为statusBarBackground的View,也就是顶部状态栏。这是明显的分层布局,所以用了FrameLayout。

    2. 动态加载Fragmemt

    具体代码见:Fragment系列:使用FrameLayout动态加载

    3. Activity.java的源码中,setContentView()的父布局

    从上图中,我们可以看大一个id为content的FragmentLayout,他内部的RelativeLayout是我自己写的activity_main.xml文件的根布局。 切换到Project模式——>找到External Libraries——>对应的编译API,例如Android API 32 Platform ——>res ——>layout文件夹下——>找到R.layout.screen_simple

    打开再核对下:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:orientation="vertical">
        <ViewStub android:id="@+id/action_mode_bar_stub"
                  android:inflatedId="@+id/action_mode_bar"
                  android:layout="@layout/action_mode_bar"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:theme="?attr/actionBarTheme" />
        <FrameLayout
             android:id="@android:id/content"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:foregroundInsidePadding="false"
             android:foregroundGravity="fill_horizontal|top"
             android:foreground="?android:attr/windowContentOverlay" />
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    这里使用FrameLayout的原因和加载Fragment是一致的,因为Activity和Fragment传递过来的都是单个布局。FrameLayout只充当一个容器。

    官网对于FrameLayout的定位是什么?

    https://developer.android.google.cn/reference/android/widget/FrameLayout

    英文原文还是更好理解一些:

    FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that’s scalable to different screen sizes without the children overlapping each other.

    You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

    Child views are drawn in a stack, with the most recently added child on top. The size of the FrameLayout is the size of its largest child (plus padding), visible or not (if the FrameLayout’s parent permits). Views that are View.GONE are used for sizing only if setConsiderGoneChildrenWhenMeasuring() is set to true.

    这段介绍强调了single,也就是说FrameLayout的设计初衷是只显示单个item。
    第二段话,讲到了,但是FrameLayout还是可以添加多个子view,然后用layout_gravity来控制位置。第三段话不太重要,不纠结。

    什么时候使用FrameLayout?

    1. 单个布局的父容器。例如自定义Activity根布局的父容器,单个Fragment也添加到FrameLayout里面。
    2. 明显分层的多个子View。例如:系统自带的DecorView。还有《疯狂Android讲义》这本书里面用FrameLayout实现的霓虹灯效果,如下图:

      源码可以参考:https://blog.csdn.net/peotry_favour/article/details/88660457。

    FrameLayout讲到这个深度,对于平时使用,已经足够了。

    推荐阅读:
    Android-DecorView-一窥全貌(下)
    Android-DecorView-一窥全貌(上)

  • 相关阅读:
    SpringBoot实现定时任务的三种方式
    AddressSanitizer(ASan)
    Hystrix超时熔断机制源码分析
    极大似然函数和似然函数的区别
    Vue-basic 06.数据代理
    微服务09-Sentinel的入门
    共建“一带一路”教育行动步入高质量发展新阶段
    [PostgreSQL的 SPI_接口函数]
    Ultralytics(YoloV8)开发环境配置,训练,模型转换,部署全流程测试记录
    一个注解@LoadBalanced就能让RestTemplate拥有负载均衡的能力?「扩展点实战系列」- 第443篇
  • 原文地址:https://blog.csdn.net/zhangjin1120/article/details/126386854
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号