什么是android:layout_weight?

Sun*_*ham 0 android android-layout

当我为一个布局设置weight = 1而对其他布局设置权重为2时会发生什么.如果我有线性布局

Dha*_*tri 6

当总和大于LinearLayout时,权重用于分配剩余的空白空间或占用空间.

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

查看有关此信息的更多信息.这里,Link1

布局重量问题

如果我们将父项划分为相等的部分,我们只需将子项的layout_weights all设置为1.但是如果我们想要将它们不等分,我们可以通过多种方式实现.我们可以使用总计1的小数小数值,或者我们可以使用整数值:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
   <LinearLayout android:background="#FF0000"
    android:layout_height="fill_parent" android:layout_width="fill_parent"
    android:layout_weight="0.66667" />
  <LinearLayout android:background="#00FF00"
    android:layout_height="fill_parent" android:layout_width="fill_parent"
    android:layout_weight="0.33333" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

要么

 <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
   <LinearLayout android:background="#FF0000"
      android:layout_height="fill_parent" android:layout_width="fill_parent"
      android:layout_weight="2" />
    <LinearLayout android:background="#00FF00"
       android:layout_height="fill_parent" android:layout_width="fill_parent"
       android:layout_weight="1" />
  </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这两者都会产生相同的结果.