我们可以使用android:layout\u weight而不将android:weightSum添加到父级吗?如果是,可能会出现什么问题?
我们可以使用android:layout\u weight而不将android:weightSum添加到父级吗?如果是,可能会出现什么问题?,android,android-layout,layout,android-linearlayout,android-layout-weight,Android,Android Layout,Layout,Android Linearlayout,Android Layout Weight,android:weightSum是否必须使用android:layout\u weight? 我们是否可以在linearlayout中直接使用android:layou-weight而不指定android:weightSum。任何人请告诉我使用android:layou-weight和不使用android:weightSum的优缺点。提前谢谢 示例代码: <LinearLayout android:layout_width="match_parent"
android:weightSum
是否必须使用android:layout\u weight
?
我们是否可以在linearlayout
中直接使用android:layou-weight
而不指定android:weightSum
。任何人请告诉我使用android:layou-weight
和不使用android:weightSum
的优缺点。提前谢谢
示例代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/table_view"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:orientation="horizontal">
<Button
android:id="@+id/button_1"
style="@style/BaseButtonStyle"
android:layout_width="0dp"
android:layout_weight="1"
android:text="@string/start" />
<Button
android:id="@+id/button_2"
style="@style/BaseButtonStyle"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="1"
android:text="@string/pause"
android:textColor="@color/teal" />
<Button
android:id="@+id/button_3"
style="@style/BaseButtonStyle"
android:layout_width="0dp"
android:layout_weight="1"
android:text="@string/stop" />
</LinearLayout>
我们可以使用android:layout\u weight而不将android:weightSum添加到
父母
是的,我们可以。考虑下面的例子-
,
如果您希望设计类似于条款和条件的行,该行右侧有TextView
和ImageButton
。它将是这样的:-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal">
<!-- Width of TextView = Width of LinearLayout - width of ImageButton -->
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?selectableItemBackgroundBorderless"
android:src="@drawable/ic_vector_back_black_18dp"/>
</LinearLayout>
对于WeightSum
的概念,您可以参考此是的,我们可以使用权重,而无需在父项中定义权重和。在这种情况下,通过将每个孩子的体重相加来计算孩子的总体重
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<ImageView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"/>
<ImageView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"/>
<ImageView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"/>
</LinearLayout>
父布局现在将自动将权重总和取为3(每个子布局的权重总和)
以下是有关此主题的更多资源
我怀疑LinearLayout没有设置属性weightsum。但是孩子们正在使用安卓:用相同或不同的重量来布置重量。在您的示例中,只有一个孩子拥有android:layout\u weight。如果LinearLayout没有weightSum,则所有子项都有重量。当您使用多个重量时,必须定义weightSum
,以便系统根据其父项(您的情况)测量-查看高度或宽度。当你想让一个孩子占据整个父母的一角硬币时,只需在这个孩子身上使用权重,其余的孩子就会自动占据他们的一角硬币(这就是我的情况)。