• 动态加载布局的技巧


    动态加载布局的技巧

    使用限定符

    • 在平板上面大多数时候采用的双页的模式,程序会在左侧列表上显示一个包含子项列表,右侧的面版会显示详细的内容的因为平板具有足够大的屏幕.完全能够显示两页的内容.但是在手机上手机只能显示一页的内容,因此需要两个页面分开显示.

      • 在运行时判断程序应该使用双页模式还是单页模式,需要借助限定符==(qualifier)==来进行实现.
      • 在layout/activity_main.xml中只包含一个Fragment,即单页模式
      
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal">
      
          <fragment
              android:id="@+id/leftFrag"
              android:name="com.zb.fragmenttest.LeftFragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_weight="1" />
          
      LinearLayout>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 创建一个layout_large目录,在这个目录下创建一个同样名为activity_main.xml的文件,但是在该布局当中包含两个Fragment,即双页模式.
      
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal">
      
          <fragment
              android:id="@+id/leftFrag"
              android:name="com.zb.fragmenttest.LeftFragment"
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1" />
      
          <fragment
              android:id="@+id/rightFrag"
              android:name="com.zb.fragmenttest.RightFragment"
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="3" />
      LinearLayout>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 解决在Android开发中layout_large目录下不能创建xml文件的方法:https://blog.csdn.net/CEVERY/article/details/86593814
      • 其中large就是一个限定符,那些屏幕被认为是large的设备就睡加载layout_large文件夹下的布局,小屏幕设备则还是会加载layout文件夹下面的布局.
      • 这样就可以实现动态加载布局的功能.
      • 安卓中常见的限定符

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Z9Ebe6T2-1669533175720)(C:/Users/zhengbo/%E6%88%91%E7%9A%84%E5%AD%A6%E4%B9%A0/Typora%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/%E5%AE%89%E5%8D%93/image-20221126163016539.png)]

    使用最小宽度限定符

    • 最小宽度限定符,允许我们对屏幕的宽度指定一个最小值(以dp为单位)
    • 然后以这个最小值为临界点.屏幕宽度大于这个值得设备就加载一个布局
    • 屏幕宽度小于这个值得就加载另外一个布局
    • 在res目录下新家一个layout-sw600dp文件夹,然后在这个文件夹下面建一个activity_main.xml布局
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    
        <fragment
            android:id="@+id/leftFrag"
            android:name="com.zb.fragmenttest.LeftFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    
        <fragment
            android:name="com.zb.fragmenttest.RightFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3" />
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 这就意味着,当程序运行在屏幕宽度大于等于600dp的设备上时,会加载layout_sw600dp/activity_main布局,当程序运行在屏幕宽度小于600dp的设备上的时候,则仍然加载默认的layout/activity_main布局.
  • 相关阅读:
    Java多态详解
    kali linux实现arp攻击对方主机
    ASP.NET Core + SaasKit + PostgreSQL + Citus 的多租户应用程序架构示例
    齐博x1 你们的 上一页 下一页 还好么?
    YOLOv8+swin_transfomer
    Pycharm连接远程服务器 导入远程包时本地显示红色解决方法
    vue3自定义指令批量注册
    Redis笔记
    vue模板语法(上)
    前端 html 中的 meta 标签有哪些用处?
  • 原文地址:https://blog.csdn.net/weixin_45809829/article/details/128064733