weight:重量、权重。

  当我们给一个view设置了android:layout_weight属性,意味着赋予它话语权,常规思维就是谁的weight大,谁说了算(空间占比大)。

android:layout_weight属性详解

 

  下面我们来看下具体的代码:

  

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:text="我的weight是2"
        android:layout_weight="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我的weight是1"
        android:layout_weight="1"/>

</LinearLayout>

 

 

 

两个Button的宽度设置的是wrap_content,此时weight属性使LinearLayout给内部的两个Button按2:1的权重分配了所有宽度空间。

官方推荐使用weight按比例分配宽度空间时,将LinearLayout内View的宽度设置为0。

相关文章:

  • 2021-09-25
  • 2021-08-14
  • 2021-12-04
  • 2021-12-21
  • 2021-12-05
  • 2021-09-10
  • 2021-08-26
  • 2021-10-22
猜你喜欢
  • 2022-01-16
  • 2021-07-23
  • 2022-02-27
  • 2022-12-23
  • 2022-12-23
  • 2021-10-12
相关资源
相似解决方案