Jan*_*usz 251 android android-linearlayout android-layout-weight
我总是在Android文档中读到这个有趣的重量值.现在我想第一次尝试它,但它根本不工作.
据我所知,这个布局的文件:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="Register"
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
weight="1" />
<Button
android:text="Not this time"
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
weight="1" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
应创建两个水平对齐的按钮,并平等分享空间.问题是两个按钮不会增长以填充空间.
我想按钮增长并填满整行.如果两个按钮都设置为仅匹配父按钮,则显示第一个按钮并填充整行.
Ank*_*nke 665
要记住的3件事:
例:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="5">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
结果如下:

小智 52
是的android:layout_weight.重量只能用于LinearLayout.如果linearlayout的方向是Vertical,则使用android:layout_height="0dp",如果方向是水平的,则使用android:layout_width = "0dp".它会很完美.
LinearLayout支持为各个孩子分配权重.此属性为视图指定" 重要性 "值,并允许它展开以填充父视图中的任何剩余空间.默认权重为零
计算以分配孩子之间的任何剩余/额外空间.(不是总空间)
空间分配给孩子=(儿童个人体重)/(线性布局中每个孩子的体重总和)
示例(1): 如果有三个文本框,其中两个声明权重为1,而第三个没有赋予权重(0),则剩余/额外空间分配给
1st text box = 1/(1+1+0)
2nd text box = 1/(1+1+0)
3rd text box = 0/(1+1+0)
Run Code Online (Sandbox Code Playgroud)
示例(2):假设我们在水平行中有一个文本标签和两个文本编辑元素.标签没有指定layout_weight,因此它占用了渲染所需的最小空间.如果两个文本编辑元素中每个文本编辑元素的layout_weight设置为1,则父布局中的剩余宽度将在它们之间平均分配(因为我们声称它们同样重要).
calculation :
1st label = 0/(0+1+1)
2nd text box = 1/(0+1+1)
3rd text box = 1/(0+1+1)
Run Code Online (Sandbox Code Playgroud)
如果第一个文本框的layout_weight为1且第二个文本框的layout_weight为2,则剩余空间的三分之一将被赋予第一个,而三分之二将被赋予第二个(因为我们声称第二个是更重要).
calculation :
1st label = 0/(0+1+2)
2nd text box = 1/(0+1+2)
3rd text box = 2/(0+1+2)
Run Code Online (Sandbox Code Playgroud)
在按钮的宽度字段中,替换wrap-content为0dp.
使用视图的layout_weight属性.
android:layout_width="0dp"
Run Code Online (Sandbox Code Playgroud)
这就是你的代码的样子:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="Register"
android:id="@+id/register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_weight="1" />
<Button
android:text="Not this time"
android:id="@+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_weight="1" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
layout_weight用于将任何左侧空间分配为比例.在这种情况下,两个按钮的宽度为"0dp".因此,剩余空间将被分为1:1比例,即空间将在按钮视图之间平均分配.
就像@Manoj Seelan的回答一样
替换android:layout_weight用android:weight.
当你使用Weight时LinearLayout.你必须添加weightSum的LinearLayout,并根据你的方向LinearLayout,你必须设置0dp所有的宽度/高度LinearLayout单曲儿童的意见
示例:
如果方向Linearlayout为Vertical,则设置所有LinearLayout子视图的宽度0dp
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="3">
<Button
android:text="Register"
android:id="@+id/register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_weight="2" />
<Button
android:text="Not this time"
android:id="@+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_weight="1" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
如果方向Linearlayout是horizontal,则设置所有LinearLayout儿童视图的高度0dp.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<Button
android:text="Register"
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:padding="10dip"
android:layout_weight="2" />
<Button
android:text="Not this time"
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:padding="10dip"
android:layout_weight="1" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
也许将两个按钮的layout_width属性都设置为“ fill_parent”可以解决问题。
我刚刚测试了这段代码,它可以在模拟器中运行:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hello world"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="goodbye world"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
确保在两个按钮上都将layout_width设置为“ fill_parent”。
| 归档时间: |
|
| 查看次数: |
373464 次 |
| 最近记录: |