zoukankan      html  css  js  c++  java
  • Android之使用weight属性实现控件的按比例分配空间

    从今天開始,把看书时候的知识点整理成博客,

    这个比較简单,预计有经验的都用过,weight属性

    在做Android布局的时候,常常遇到须要几个控件按比例分配空间的情况

    比方下图效果

    在底部设置两个button,占领底部宽度一部分的同一时候,保持1:3的比例,

    当然了,这么难看的布局用处不大,仅是用来说明weight的使用方法

    布局代码例如以下:

    <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:weightSum="6"
        android:gravity="bottom|center_horizontal" >
    
        <Button
            android:id="@+id/bn_main_left"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="left" />
    
        <Button
            android:id="@+id/bn_main_right"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="right" />
    
    </LinearLayout>


    当中LinearLayout里面有个weightSum,这个属性是用来设置LinearLayout的weight总和,

    Button里面的layout_weight就是用来设置button占领LinearLayout的空间的大小

    形象一点说,LinearLayout像一个盒子,weightSum设置了盒子的大小为6,

    往盒子里放了两个button,给左边button设置layout_weight="1",占领1/6空间,

    右边button设置了layout_weight="3",占领3/6空间

    这样两个button加起来占领了LinearLayout的4/6,

    假设没有给LinearLayout设置weightSum的话,则默觉得全部控件layout_weight的总和.

    作者:jason0539

    微博:http://weibo.com/2553717707

    博客:http://blog.csdn.net/jason0539(转载请说明出处)

  • 相关阅读:
    shell命令执行过程
    shell中的引用
    ansible小结
    centos自动化安装镜像制作
    centos kickstart
    centos内核引导参数
    C# .NET 遍历Json 形成键值对
    强大的Winform Chart图表控件使用说明
    C#实现转换十六进制
    C# 批量登陆远程目录
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/6917911.html
Copyright © 2011-2022 走看看