zoukankan      html  css  js  c++  java
  • 日积月累:weightSum和layout_weight属性合用

    解说一: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父控件的宽度; 

     

     

  • 相关阅读:
    Python学习 Day 068
    Python模块(一)(常用模块)
    python面向对象(C3算法)(六)
    Python面向对象(约束,异常处理,md5加密)(五)
    python面向对象(反射)(四)
    Python面向对象(类之间的关系)(三)
    Python面向对象(成员)(二)
    python面向对象(一)
    Python函数的装饰器
    递归与二分法
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5092488.html
Copyright © 2011-2022 走看看