我正在使用layout_weight
来指定android中特定viewGroups
中各种视图的比率。
从this问题的答案我得到了关于layout_weight
是什么的清晰概念。我使用正常的数学计算所有views
in 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”的视图大小?
对于所有具有layout_weight的视图,必须将layout_height或layout_width设置为0dp,具体取决于布局的方向和要求。
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之外,它将以适当的方式显示。
对于所有具有layout_weight的视图,必须将layout_height或layout_width设置为0dp,具体取决于布局的方向和要求。
那不对。首先,应用“layout_width”和“layout_height”参数,并且视图将至少为此大小。其次,ViewGroup中的剩余空间将根据其权重按比例分配。因此,权重“0”表示在该阶段期间不会给视图增加一些额外的大小。