• Android LinearLayout的android:layout_weight属性


    本文主要介绍Android LinearLayout的android:layout_weight属性意义

    android:layout_weight为大小权重,相当于在页面上显示的百分比,它的计算是根据LinearLayout中所有相关元素的此属性值计算的。

    除了已经固定大小的,其他设置了此属性的view所占大小(长度或高度)为自己layout_weight属性值/所有layout_weight属性值*总大小。这个属性在android的sdk中都没有介绍。下面举例介绍下

    比如在一个layout中显示3个TextView,第一个TextView长度占20%,第二个长度占50%,第三个占长度30%,

    则比例为20%:50%:30%=2:5:3。layout代码如下

    Java代码  收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout  
    3.     xmlns:android="http://schemas.android.com/apk/res/android"  
    4.     android:layout_width="match_parent"  
    5.     android:layout_height="match_parent">     
    6.     <TextView android:id="@+id/textView1"     
    7.         android:layout_width="wrap_content"     
    8.         android:layout_height="match_parent"     
    9.         android:layout_weight="2"  
    10.         android:layout_alignParentLeft="true"  
    11.         android:gravity="center_vertical"  
    12.         android:text="文本1" />   
    13.     <TextView android:id="@+id/textView2"     
    14.         android:layout_width="wrap_content"     
    15.         android:layout_height="match_parent"     
    16.         android:layout_weight="5"  
    17.         android:layout_alignParentLeft="true"  
    18.         android:gravity="center_vertical"  
    19.         android:text="文本2" />   
    20.     <TextView android:id="@+id/textView3"     
    21.         android:layout_width="wrap_content"     
    22.         android:layout_height="match_parent"     
    23.         android:layout_weight="3"  
    24.         android:layout_alignParentLeft="true"  
    25.         android:gravity="center_vertical"  
    26.         android:text="文本3" />   
    27. </LinearLayout>  

    从以上代码可以看出只需要设置各个TextView的android:layout_weight属性值为对应的比例即可

    其中android:layout_alignParentLeft="true" android:gravity="center_vertical"是为了方便查看而设置

  • 相关阅读:
    React 组件
    React JSX
    React基础
    equals和hashCode详解
    Hibernate 二级缓存配置
    如何正确地停止一个线程?
    常见的异常以及常用的包,类,及其接口。
    5.水果
    Java -- Web前端面试题及答案(需更深入了解)
    微信access_token请求之简单缓存方法封装
  • 原文地址:https://www.cnblogs.com/xgjblog/p/3838307.html
Copyright © 2020-2023  润新知