必威体育Betway必威体育官网
当前位置:首页 > IT技术

Android-0.Android Studio布局中layout_weight用法

时间:2019-09-06 12:10:00来源:IT技术作者:seo实验室小编阅读:90次「手机版」
 

layout_weight

 Indicates how much of the extra space in the Linearlayout is allocated to the view associated with these layoutparams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.

指示LinearLayout中多少额外空间分配给与这些LayoutParams关联的视图。 如果视图不应被拉伸,请指定0。 否则,额外空间将在权重大于0的所有视图中按比例分配。

上面有几点:

1.额外空间,指的是剩余空闲空间, 额外空间将在权重大于0的所有视图中按比例分配。

如下,总权重为1+1=2

第一个控件是比第二个控件占的空间小的,即w(12345)+1/2空闲空间< w(123456)+1/2控件

<LinearLayout 
  Android:orientation="horizontal">
  <TextView 
      android:layout_width="wrap_content"
      android:layout_height="wrap_height"
      android:layout_weight="1"
      android:text="12345"/>

  <TextView 
      android:layout_width="wrap_content"
      android:layout_height="wrap_height"
      android:layout_weight="1"
      android:text="123456"/>
</LinearLayout>

如果我们让控件的宽度定义为layout_width="0dp" ,这样比如2个控件的 layout_weight="1" 就可以各自50%平分整个空间了,因为:0 + 1/2空闲空间 = 0 + 1/2空闲空间。

2.默认layout_weight为0,所以如果这么写:

<LinearLayout 
    android:orientation="horizontal">

    <TextView
        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"/>

    <TextView
        android:layout_width="40dp"
        android:layout_height="match_parent"
        android:background="#888" />

</LinearLayout>

则总权重为1,即Button占了所有剩余空闲空间,无论它在哪个位置

3.在排列方向上设置了match_parent, 如下,权重为2,1,2

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:text="1"
            android:layout_weight="2"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2"
            android:layout_weight="1"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="3"
            android:layout_weight="2"/>

运行结果如下:

分析:因为设置的都是match_parent,所以如果没有设置权重,三个Button只会显示第一个,其他会被覆盖,但是设置了权重后, 我们就按三个Button给定的width=match_parent计算剩余空间

剩余空间=1个match_parent空间-3个match_parent空间= -2个match_parent空间(负2)

所以

Button1所占空间 = 1个match_parent空间+(-2个match_parent空间)*2/5 = 1/5个match_parent空间

Button2所占空间 = 1个match_parent空间+(-2个match_parent空间)*1/5 = 3/5个match_parent空间

Button3所占空间 = 1个match_parent空间+(-2个match_parent空间)*2/5 = 1/5个match_parent空间

所以在统一设置match_parent时,会有这么一个特性,权重越大,空间越小。

而且在某个控件权重刚好为另外的所有控件权重之和时,这个控件会消失。

如权重变为1,2,3

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:text="1"
            android:layout_weight="2"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2"
            android:layout_weight="1"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="3"
            android:layout_weight="2"/>

运行结果如下:

同样的算法

Button1所占空间 = 1个match_parent空间+(-2个match_parent空间)*1/6 = 2/3个match_parent空间

Button2所占空间 = 1个match_parent空间+(-2个match_parent空间)*2/6 = 1/3个match_parent空间

Button3所占空间 = 1个match_parent空间+(-2个match_parent空间)*3/6 = 0个match_parent空间

剩余布局大小 = 父布局大小 - 子布局大小之和

相关阅读

layout_weight用法以及注意事项

1、实现了水平方向50%平均分割 <LinearLayout android:orientation="horizontal"> <TextView android:layout_widt

LinearLayout的layout_weight属性的计算

layout_weight表示view分配到的额外的布局空间,可正可负。布局的时候,系统先按照view的layout_width和layout_height来布局,然后再根

Android:layout_weight的使用和坑

问题今天做项目有一个需求,一个TextView单行显示,多出来的字使用跑马灯效果,然后一个TextView后面要跟一个图片,当时想着怎么弄好呢,文

Android布局基础知识:wrap_content,match_parent,layou

wrap_content:是layout_width和layout_height的属性值之一,表示和自身内容一样的长度。match_parent:是layout_width和layout_heig

分享到:

栏目导航

推荐阅读

热门阅读