Android动态加载布局实现技巧介绍
作者:z啵唧啵唧 时间:2022-03-06 13:31:48
使用限定符
在平板上面大多数时候采用的双页的模式,程序会在左侧列表上显示一个包含子项列表,右侧的面版会显示详细的内容的因为平板具有足够大的屏幕.完全能够显示两页的内容.但是在手机上手机只能显示一页的内容,因此需要两个页面分开显示.
在运行时判断程序应该使用双页模式还是单页模式,需要借助限定符==(qualifier)==来进行实现.
在layout/activity_main.xml中只包含一个Fragment,即单页模式
<?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/leftFrag"
android:name="com.zb.fragmenttest.LeftFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
创建一个layout_large目录,在这个目录下创建一个同样名为activity_main.xml的文件,但是在该布局当中包含两个Fragment,即双页模式.
<?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/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>
解决在Android开发中layout_large目录下不能创建xml文件的方法:https://blog.csdn.net/CEVERY/article/details/86593814
其中large就是一个限定符,那些屏幕被认为是large的设备就睡加载layout_large文件夹下的布局,小屏幕设备则还是会加载layout文件夹下面的布局.
这样就可以实现动态加载布局的功能.
安卓中常见的限定符
使用最小宽度限定符
最小宽度限定符,允许我们对屏幕的宽度指定一个最小值(以dp为单位)
然后以这个最小值为临界点.屏幕宽度大于这个值得设备就加载一个布局
屏幕宽度小于这个值得就加载另外一个布局
在res目录下新家一个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/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>
这就意味着,当程序运行在屏幕宽度大于等于600dp的设备上时,会加载layout_sw600dp/activity_main布局,当程序运行在屏幕宽度小于600dp的设备上的时候,则仍然加载默认的layout/activity_main布局.
来源:https://blog.csdn.net/weixin_45809829/article/details/128064733
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java String转换时为null的解决方法
android获取屏幕高度和宽度的实现方法
C#使用foreach语句遍历二维数组的方法
JAVA十大排序算法之桶排序详解
![](https://img.aspxhome.com/file/2023/8/66678_0s.png)
C#中按引用传递与按值传递的区别,以及ref与out关键字的用法详解
Android实现截屏功能
java操作json对象出现StackOverflow错误的问题及解决
![](https://img.aspxhome.com/file/2023/2/63162_0s.png)
Java Swing中的JButton、JComboBox、JList和JColorChooser组件使用案例
![](https://img.aspxhome.com/file/2023/1/77051_0s.jpg)
SpringCloud之分布式配置中心Spring Cloud Config高可用配置实例代码
![](https://img.aspxhome.com/file/2023/8/85408_0s.png)
Java内存模型可见性问题相关解析
java中的JsonSerializer用法,前后端单位转换必备
![](https://img.aspxhome.com/file/2023/4/64904_0s.jpg)
使用mybatis-plus想要修改某字段为null问题
C#实现调用本机摄像头实例
java注解的类型知识点总结
java实现Xml与json之间的相互转换操作示例
![](https://img.aspxhome.com/file/2023/6/100666_0s.png)
SpringMVC统一异常处理实例代码
java判断list不为空的实现,和限制条数不要在一起写
面试题:Java 实现查找旋转数组的最小数字
Java中的多态用法实例分析
![](https://img.aspxhome.com/file/2023/8/77098_0s.png)
Java中如何使用Response重定向
![](https://img.aspxhome.com/file/2023/9/71249_0s.png)