android- layout_weight =“0”的含义

问题描述 投票:3回答:3

我正在使用layout_weight来指定android中特定viewGroups中各种视图的比率。 从this问题的答案我得到了关于layout_weight是什么的清晰概念。我使用正常的数学计算所有viewsin a viewGroup的大小(即我有3个views的1,2和3个layout_weights并且他们都有layout_height =“0dp”然后他们有1 /(1 + 2 + 3),2 / (1 + 2 + 3),其viewGroup中的3 /(1 + 2 + 3)个空格用于Verical对齐)。 但是,layout_weight =“0”是什么意思?如何确定layout_weight =“0”的视图大小?

android xml android-layout android-view android-layout-weight
3个回答
1
投票

对于所有具有layout_weight的视图,必须将layout_height或layout_width设置为0dp,具体取决于布局的方向和要求。

  1. layout_weight =“1”和layout_width =“0dp”==>如果没有其他布局,则特定视图将水平拉伸。
  2. layout_weight =“0”和layout_width =“100dp”==>该特定布局的行为与此场景中没有layout_weight的含义相同。
  3. 最好使用重量是当您需要两个具有相同高度/宽度的视图时,您可以将布局和重量的宽度/高度添加为“0dp”,两个布局都为“1”。

0
投票

layout_weight =“0”在xml中没有意思应该有android:layout_width =“0dp”所以如果你想在Linarlayout方向为所有控件提供相同的空间我们使用这个例如: - 如果我们想在水平方向取3个按钮我们使用下面的代码

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

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

</LinearLayout>`

所以我们放在这里

机器人:weightSum = “1”

并且控制权重相等。在所有设备中,除了Imageview之外,它将以适当的方式显示。


0
投票

对于所有具有layout_weight的视图,必须将layout_height或layout_width设置为0dp,具体取决于布局的方向和要求。

那不对。首先,应用“layout_width”和“layout_height”参数,并且视图将至少为此大小。其次,ViewGroup中的剩余空间将根据其权重按比例分配。因此,权重“0”表示在该阶段期间不会给视图增加一些额外的大小。

© www.soinside.com 2019 - 2024. All rights reserved.