码农知识堂 - 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 学习笔记
    数据结构:链表
    Vue脚手架的搭建
    python2中安装virtualenv并创建虚拟环境
    No.6软件工程之需求分析
    【计算机毕业设计】基于JAVA的在线租房网站系统源码
    会计学期末题库 含WORD版
    Pytest系列-数据驱动@pytest.mark.parametrize(7)
    1856. 子数组最小乘积的最大值-前准和+单调栈
    iPhone 15 换 USB-C 或藏“心机”,爆料者:只有 Pro 版提速,其他限速 USB 2.0
  • 原文地址:https://blog.csdn.net/zhangjin1120/article/details/126386854
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号