动态加载布局

今天在学习第二行代码的时候遇到一个问题,在碎片化的这一节有一个动态加载布局。所谓动态加载布局就是为了兼容手机和平板尺寸上的大一不一。使用动态加载布局的方式程序会根据设备的分辨率或者屏幕大小在运行的时候进行判断来加载哪一个布局。先看实现效果:
手机:
这里写图片描述
平板:
这里写图片描述
实现方法就是有2个activity_main.xml文件,一个在res/layout/下面,一个在res/large/下面。
res/layout/activity_main.xml:

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.tony.fragmenttest.MainActivity">

    <fragment
        android:id="@+id/left_fragment"
        android:name="com.example.tony.fragmenttest.LeftFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <!--&lt;!&ndash;<fragment
        android:id="@+id/right_fragment"
        android:name="com.example.tony.fragmenttest.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"&ndash;&gt;
        android:layout_weight="1" />-->

   <!-- <FrameLayout
        android:id="@+id/right_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

    </FrameLayout>-->

</LinearLayout>

res/large/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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/left_fragment"
        android:name="com.example.tony.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/right_fragment"
        android:name="com.example.tony.fragmenttest.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" />

</LinearLayout>

使用最小宽度限定符这一个需要新建一个layout-sw600dp的文件夹来存放xml文件,一开始自己折腾好久发现在这个文件夹下面无法新建layout布局文件,后来才发现是文件名称输入错误了,中间的分隔符不是下划线_,而是-。就是layout_se600dp跟layout-sw600dp的区别。所以敲代码这个东西还是需要仔细,不然一个符号一个字母打错都会让我们找半天错误而又发现不了哪里错了。然后在这个文件夹下面新建activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<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/left_fragment"
        android:name="com.example.tony.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@id/right_fragment"
        android:name="com.example.tony.fragmenttest.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" />

</LinearLayout>

这样当程序运行在屏幕宽度大于600的设备就会加载这个布局文件,反之则会加载res/layout/activity_main.xml这个布局文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值