动态加载布局

2016-04-22  本文已影响225人  TTTqiu

一、使用限定符(Qualifiers)


1. 修改 activity_main.xml 文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <fragment
        android:id="@+id/left_fragment"
        android:name="com.example.fragmenttest.LeftFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
2. 在 res 目录下新建 layout-large 文件夹,在这个文件夹下新建一个布局,也叫做activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <fragment
        android:id="@+id/left_fragment"
        android:name="com.example.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.fragmenttest.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" />
</LinearLayout>
3. 在平板上运行:
4. 在手机上运行:
5. 常见限定符:
屏幕特征 限定符 描述
大小 small 提供给小屏幕设备的资源
大小 normal 提供给中等屏幕设备的资源
大小 large 提供给大屏幕设备的资源
大小 xlarge 提供给超大屏幕设备的资源
分辨率 ldpi 提供给低分辨率设备的资源(120dpi 以下)
分辨率 mdpi 提供给中等分辨率设备的资源(120dpi 到160dpi)
分辨率 hdpi 提供给高分辨率设备的资源(160dpi 到240dpi)
分辨率 xhdpi 提供给超高分辨率设备的资源(240dpi 到320dpi)
方向 land 提供给横屏设备的资源
方向 port 提供给竖屏设备的资源

二、使用最小宽度限定符(Smallest-width Qualifier)


1. 新建 layout-sw600dp 文件夹。
2. 当程序运行在屏幕宽度大于 600dp 的设备上时,会加载 layout-sw600dp/activity_main 布局。
3. 当程序运行在屏幕宽度小于 600dp 的设备上时,则仍然加载默认的 layout/activity_main 布局。
上一篇 下一篇

猜你喜欢

热点阅读