码迷,mamicode.com
首页 > 其他好文 > 详细

日积月累:weightSum和layout_weight属性合用

时间:2015-12-31 19:07:42      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

解说一:weightSum和layout_weight属性合用 

  • android:weightSum属性:定义weight总和的最大值。

    假设为指定该值,全部子视图的layout_weight属性的累加值作为总和的最大值。比如。通过指定子视图的layout_weight属性为0.5。并设置LinearLayout的weight属性为1.0,实现子视图占领可用宽度的50%; 

  • layout_weight属性:子视图的控件比例。就像我们在盒子放置其它物体,盒子可用空间的比例就是weightSum,盒子每一个控件的比例就是layout_weight。 

 

以下我们通过一个详细的需求,结合两个属性实现。需求例如以下:居中显示button,并占领父控件的50%宽度。

编写布局文件main_activity.xml文件例如以下: 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal" 
    android:weightSum="1.0" 
    tools:context=".MainActivity" > 
    <Button 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="0.5" 
        android:text="@string/hello_world" /> 
</LinearLayout> 

效果例如以下图 

技术分享


























分析例如以下 

  1. android:gravity="center_horizontal":实现button居中显示 

  2.  android:weightSum="1.0"、android:layout_width="0dp"和android:layout_weight="0.5":button的宽度=button本身的宽度+button的weight*父控件的宽度/父控件的weightSum;即:button的宽度=0dp+0.5*父控件的宽度/1.0=0.5父控件的宽度; 

 

 

日积月累:weightSum和layout_weight属性合用

标签:

原文地址:http://www.cnblogs.com/mengfanrong/p/5092488.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!