有人可以解释这个权重是如何工作的吗?为什么

时间:2018-02-22 10:21:51

标签: android android-layout

当我检查此代码的设计时。这个WeightSum行为与我想要的方式完全相反。当我设置我的按钮weightSum为70时,它需要30(总重量是100)反之亦然。

  <LinearLayout
         android:weightSum="100"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         >

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

             <ToggleButton
                 android:layout_weight="30"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="ToggleButton" />


    </LinearLayout>

5 个答案:

答案 0 :(得分:3)

您需要设置按钮android:layout_width="0dp" ToggleButton

答案 1 :(得分:1)

如果LinearLayout方向为水平,则设置子视图的宽度= 0,如果方向为垂直,则设置高度= 0 我已对您的代码进行了更改,请参阅:)

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

             <Button
                 android:layout_weight="70"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:text="Button" />

             <ToggleButton
                 android:layout_weight="30"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:text="ToggleButton" />


    </LinearLayout>

它以百分比为基础,当您想要发表您的观点时高度和宽度占据其父母的一定百分比然后权重是解决方案 例如如果weightSum = 1个孩子可以是weight_layout .30和.60。所以基本上增加了孩子们的孩子。 layout_weight应该等于其父级的weightSum(或者在WRT用例中更少)

答案 2 :(得分:1)

因此android:weightSum定义Layout的最大权重总和,并计算其所有子视图的layout_weight的总和。

示例: - 具有3个视图的LinearLayout(可以是任何内容)。现在,您希望在屏幕中平均显示3个视图。因此,需要将layout_weight放入视图1,而weightSum为3。

<LinearLayout
         android:weightSum="100"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         >

             <View
                 android:layout_weight="70"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:text="Button" />

             <View
                 android:layout_weight="30"
                 android:layout_width="fill_parent"
                 android:layout_height="0dp`enter code here`"
                 android:text="ToggleButton" />


    </LinearLayout>

或者您也可以将android:layout_weight放在points中,如下所示: -

<LinearLayout
         android:weightSum="1"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         >

             <View
                 android:layout_weight=".7"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:text="Button" />

             <View
                 android:layout_weight=".3"
                 android:layout_width="fill_parent"
                 android:layout_height="0dp`enter code here`"
                 android:text="ToggleButton" />


    </LinearLayout>

使用前请记住3件事android:weightSum: -

  
      
  1. 将孩子的android:layout_width设置为“0dp”

  2.   
  3. 设置父级的android:weightSum(编辑:正如Jason Moore注意到的,这个属性是可选的,因为默认情况下它设置为   孩子们的layout_weight总和)

  4.   
  5. 按比例设置每个孩子的android:layout_weight(例如weightSum =“5”,三个孩子:layout_weight =“1”,layout_weight =“3”,   layout_weight = “1”)

  6.   

答案 3 :(得分:0)

基于方向,您应将父级布局的layout_heightlayout_width设置为 0dp ,以确保权重的正确行为。

答案 4 :(得分:0)

为了使其运作良好,正如其他人指出的那样,宽度(或高度,如果LinearLayout方向是垂直的)应该是0(px或dp,无论你想要什么,因为它仍然是0)对于有体重的孩子。

关于它是如何工作的解释,如果不是所有都有重量,剩下的一个将传播给它们。

在您的情况下,您根本不必设置weightSum,因为您已经为每个设置了权重(70 + 30确实为100)。

它将做什么(在所需的更改之后),将第一个View设置为宽度为父级宽度的30%,第二个视图的宽度为父级宽度的70%。

相关问题