Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3108
  • 博文数量: 2
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 25
  • 用 户 组: 普通用户
  • 注册时间: 2015-08-30 09:46
文章分类
文章存档

2015年(2)

我的朋友
最近访客

分类: Android平台

2015-08-31 09:33:49

一、细说layout_weight
    目前最为推荐的Android多屏幕自适应解决方案。
    该属性的作用是决定控件在其父布局中的显示权重,一般用于线性布局中。其值越小,则对应的layout_width或layout_height的优先级就越高,一般横向布局中,决定的是layout_width的优先级;纵向布局中,决定的是layout_height的优先级。
    传统的layout_weight使用方法是将当前控件的layout_widthlayout_height都设置成fill_parent,这样就可以把控件的显示比例完全交给layout_weight;这样使用的话,就出现了layout_weight越小,显示比例越大的情况。不过对于2个控件还好,如果控件过多,且显示比例也不相同的时候,控制起来就比较麻烦了,毕竟反比不是那么好确定的。
    于是就有了现在最为流行的0px设值法。看似让人难以理解的layout_height=0px的写法,结合layout_weight,却可以使控件成正比例显示,轻松解决了当前Android开发最为头疼的碎片化问题之一。
    先看下面的stylesstyle_layout.xml


点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.  
  4. <!-- 全屏幕拉伸-->
  5.   <style name="layout_full">
  6.     <item name="android:layout_width">fill_parent</item>
  7.     <item name="android:layout_height">fill_parent</item>
  8.   </style>
  9.    
  10. <!-- 固定自身大小-->
  11.   <style name="layout_wrap">
  12.     <item name="android:layout_width">wrap_content</item>
  13.     <item name="android:layout_height">wrap_content</item>
  14.   </style>
  15.  
  16. <!-- 横向分布-->
  17.   <style name="layout_horizontal" parent="layout_full">
  18.     <item name="android:layout_width">0px</item>
  19.   </style>
  20.     
  21. <!-- 纵向分布-->
  22.   <style name="layout_vertical" parent="layout_full">
  23.     <item name="android:layout_height">0px</item>
  24.   </style>
  25.          
  26. </resources>
可以看到,layout_widthlayout_height两个属性被我封装成了4style
    根据实际布局情况,选用当中的一种,不需要自己设置,看过我前一个ActivityGroupDemo的同学应该非常熟悉了
    然后我的Demo的布局如下(weight_layout.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=" />
  3.         style="@style/layout_full"
  4.         android:orientation="vertical">
  5.         <LinearLayout
  6.                 style="@style/layout_vertical"
  7.                 android:layout_weight="1"
  8.                 android:orientation="horizontal">
  9.                  <View
  10.                          style="@style/layout_horizontal"
  11.                          android:background="#aa0000"
  12.                          android:layout_weight="1"/>
  13.                  <View
  14.                          style="@style/layout_horizontal"
  15.                          android:background="#00aa00"
  16.                          android:layout_weight="4"/>
  17.                  <View
  18.                          style="@style/layout_horizontal"
  19.                          android:background="#0000aa"
  20.                          android:layout_weight="3"/>
  21.                  <View
  22.                          style="@style/layout_horizontal"
  23.                          android:background="#aaaaaa"
  24.                          android:layout_weight="2"/>
  25.         </LinearLayout>
  26.         <LinearLayout
  27.                 style="@style/layout_vertical"
  28.                 android:layout_weight="2"
  29.                 android:orientation="vertical">
  30.                 <View
  31.                          style="@style/layout_vertical"
  32.                          android:background="#ffffff"
  33.                          android:layout_weight="4"/>
  34.                  <View
  35.                          style="@style/layout_vertical"
  36.                          android:background="#aa0000"
  37.                          android:layout_weight="3"/>
  38.                  <View
  39.                          style="@style/layout_vertical"
  40.                          android:background="#00aa00"
  41.                          android:layout_weight="2"/>
  42.                  <View
  43.                          style="@style/layout_vertical"
  44.                          android:background="#0000aa"
  45.                          android:layout_weight="1"/>
  46.  
  47.         </LinearLayout>
  48. </LinearLayout>

整个界面布局看起来非常直观,只是嵌套的逻辑要自己理下。显示效果如下图,其中左面一个是480x800的界面,右面的是320x480的界面(后面的图也如此),可以看出显示比例和代码中完全一致,我就不多说了,大家对照下就能看出来了。
阅读(270) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~