Android中LinearLayout布局的常用属性总结

2019-12-10 18:19:35丽君

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1"
    android:layout_weight="1"/>

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 2"
    android:layout_weight="1"/>

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 3"
    android:layout_weight="1"/>

</LinearLayout>

比重都是 1,所以大小相同。


非等比例分配

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1"
    android:layout_weight=".10"/>

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 2"
    android:layout_weight=".20"/>
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 3"
    android:layout_weight=".70"/>

</LinearLayout>

.10 代表 0.10
.20 代表 0.20
.70 代表 0.70
合起来刚好是 1 ,作 100% 分配。     



注:相关教程知识阅读请移步到Android开发频道。