• android:layout_weight总有你不知道的用法.


      都知道weight是权重的意思. 在布局中起到非常重要的作用. 但是这玩意不能嵌套使用, 而且只能使用在LinearLayout中.

      下面说说它的几种用法(以下例子全为横排 注意android:layout_width值和android:layout_weight值的变化)  

      第一种, 最普遍的-----均分, weight的值越大, 占的空间越大.注意android:layout_width的值都为0dp

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#000" />
    
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    
        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#888" />
    
    </LinearLayout>

       第二种, 占满剩余空间, 不管是否处在最后一个

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <FrameLayout
            android:layout_width="40dp"
            android:layout_height="match_parent"
            android:background="#000" />
    
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    
        <FrameLayout
            android:layout_width="40dp"
            android:layout_height="match_parent"
            android:background="#888" />
    
    </LinearLayout>

       第三种, 值越大占的空间越小, 值为同布局下其它控件的weight之和时,即消失.注意android:layout_width的值都为match_parent为0时占满. 有什么用途? 呵呵, 存在即是合理的. 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#000" />
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.5"/>
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#000" />
    
    </LinearLayout>

       最后一种(我会用的), 结合android:weightSum使用, 那这儿就用一个问题引入, 如何将控件放在屏幕的中央, 宽度为屏幕的一半?  嗯.....想啊.....想啊...RelativeLayout? 哦, 没法控制? 哦,只能代码了... 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:weightSum="2">
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    
    </LinearLayout>

     ok, so easy!

    欢迎分享转载,转载请注明出处 http://www.cnblogs.com/fangyucun
  • 相关阅读:
    LeetCode:2. 两数相加
    LeetCode:1. 两数之和
    property类的使用
    JAVA-数据库连接【转】
    快速将excel数据保存到Oracle数据库中【转】
    Oracle导入excel数据方法汇总[转]
    Maven 系列 一 :Maven 快速入门及简单使用【转】
    Maven 系列 二 :Maven 常用命令,手动创建第一个 Maven 项目【转】
    开源项目导入eclipse的一般步骤[转]
    JavaScript基础---作用域,匿名函数和闭包【转】
  • 原文地址:https://www.cnblogs.com/fangyucun/p/3535536.html
Copyright © 2020-2023  润新知