一、简介
本篇介绍如何使用限定符。
二、实验平台
系统版本:Windows7 家庭普通版 32位操作系统。
三、版权声明
博主:思跡
声明:喝水不忘挖井人,转载请注明出处。
原文地址:http://blog.csdn.net/omoiato
联系方式:315878825@qq.com
Java零基础入门交流群:541462902
四、使用限定符
1、为什么要使用限定符?
因为屏幕大小的区别,平板电脑为双页模式,手机为单页模式,
使用限定符,可以在程序运行时判断应该是使用双页模式还是单页模式。
2、实例讲解限定符的用法:
①修改FragmentTest项目中的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>
这里将多余的代码都删掉,只留下一个左侧碎片,并让它充满整个父布局。
<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>
可以看到,layout/activity_main 布局只包含了一个碎片,即单页模式,而layout-large/
activity_main 布局包含了两个碎片,即双页模式。其中large 就是一个限定符,那些屏幕被认
为是large 的设备就会自动加载layout-large 文件夹下的布局,而小屏幕的设备则还是会加载
layout 文件夹下的布局。
③将MainActivity 中按钮点击事件的代码屏蔽掉,并在平板模拟器上重新运行程序
④再启动一个手机模拟器,并在这个模拟器上重新运行程序
这样我们就实现了在程序运行时动态加载布局的功能。
3、Android 中一些常见的限定符