Android 中的线性布局和权重
我总是在 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>
应该创建两个水平对齐并平等共享空间的按钮。问题是这两个按钮无法增大以填充空间。
我希望按钮能够增长并填满整行。如果两个按钮都设置为与父级匹配,则仅显示第一个按钮并填充整行。
I always read about this funny weight value in the Android documentations.
Now I want to try it for the first time but it isn't working at all.
As I understand it from the documentations this layout:
<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>
should create two buttons that are horizontally aligned and share the space equally. The problem is the two buttons don't grow to fill the space.
I would like the buttons to grow and fill the whole line. If both buttons are set to match parent only the first button is shown and fills the whole line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
需要记住的 3 件事:
(编辑: 正如 Jason Moore 所注意到的,此属性是可选的,因为默认情况下它设置为子级的layout_weight 总和)
示例:
结果:
3 things to remember:
(edit: as Jason Moore noticed, this attribute is optional, because by default it is set to the children's layout_weight sum)
Example:
And the result:
您没有设置
layout_weight
属性。您的代码为weight="1"
,并且应为android:layout_weight="1"
。You are not setting the
layout_weight
property. Your code readsweight="1"
and it should readandroid:layout_weight="1"
.它是
android:layout_weight
。权重只能在LinearLayout
中使用。如果 LinearLayout 的方向是垂直的,则使用android:layout_height="0dp"
;如果方向是水平的,则使用android:layout_width = "0dp"
。它会完美地工作。It's
android:layout_weight
. Weight can only be used inLinearLayout
. If the orientation of linearlayout is Vertical, then useandroid:layout_height="0dp"
and if the orientation is horizontal, then useandroid:layout_width = "0dp"
. It'll work perfectly.该图总结了线性布局。
您可以点击此链接以获取有关该主题的更多信息。 只是数学 - 视图、视图组和布局
线性布局视频教程:宽度、高度和宽度权重
Android 线性布局教程
This image summarizes the Linear layout.
You can follow this link for more information on the topic. Just Maths - Views, View Groups and Layouts
Video Tutorial For Linear Layout : Width, Height & Weights
Android Linear Layout Tutorial
尝试将两个按钮的
layout_width
设置为“0dip”,将两个按钮的weight
设置为0.5
Try setting the
layout_width
of both buttons to "0dip" and theweight
of both buttons to0.5
LinearLayout 支持为各个子元素分配权重。此属性为视图分配“重要性”值,并允许其扩展以填充父视图中的任何剩余空间。默认权重为零
计算,以分配子项之间的任何剩余/额外空间。 (不是总空间)
分配给子项的空间=(子项单独权重)/(线性布局中每个子项权重之和)
示例(1):
如果有三个文本框,其中两个声明权重为 1,而第三个文本框未指定权重 (0),则将剩余/额外空间分配给
示例 (2)< /strong> :假设我们在水平行中有一个文本标签和两个文本编辑元素。该标签没有指定layout_weight,因此它占用渲染所需的最小空间。如果两个文本编辑元素中的每一个的layout_weight都设置为1,则父布局中的剩余宽度将在它们之间平均分配(因为我们声称它们同样重要)。
如果第一个文本框的layout_weight为1,第二个文本框的layout_weight为2,则剩余空间的三分之一将分配给第一个,三分之二分配给第二个(因为我们声称第二个文本框是更重要)。
LinearLayout supports assigning a weight to individual children. This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view. Default weight is zero
calculation to assign any Remaining/Extra space between child. (not the total space)
space assign to child = (child individual weight) / (sum of weight of every child in Linear Layout)
Example (1):
if there are three text boxes and two of them declare a weight of 1, while the third one is given no weight (0), then Remaining/Extra space assign to
Example (2) : let's say we have a text label and two text edit elements in a horizontal row. The label has no layout_weight specified, so it takes up the minimum space required to render. If the layout_weight of each of the two text edit elements is set to 1, the remaining width in the parent layout will be split equally between them (because we claim they are equally important).
If the first one text box has a layout_weight of 1 and the second text box has a layout_weight of 2, then one third of the remaining space will be given to the first, and two thirds to the second (because we claim the second one is more important).
在按钮的宽度字段中,将
wrap-content
替换为0dp
。使用视图的layout_weight属性。
您的代码如下所示:
layout_weight 用于将剩余空间按比例分配。在本例中,两个按钮的宽度为“0dp”。因此,剩余空间将按照 1:1 的比例进行划分,即空间将在 Button View 之间平分。
In the width field of button, replace
wrap-content
with0dp
.Use layout_weight attribute of a view.
This is how your code will look like:
layout_weight is used to distribute the whatever left space into proportions. In this case, the two buttons are taking "0dp" width. So, the remaining space will be divided into 1:1 proportion among them, i.e. the space will be divided equally between the Button Views.
就像 @Manoj Seelan 的回答一样,
将
android:layout_weight
替换为android:weight.
当您将 Weight 与
LinearLayout
结合使用时。您必须在LinearLayout
中添加weightSum
,并且根据LinearLayout
的方向,您必须将宽度/高度设置为0dp
所有LinearLayout
的子视图示例:
如果
Linearlayout
的方向是Vertical
,则设置所有<的宽度code>LinearLayout 的子视图0dp
如果 的方向
Linearlayout
是horizontal
,那么使用0dp
设置所有 LinearLayout 的子视图的高度。Like answer of @Manoj Seelan
Replace
android:layout_weight
Withandroid:weight
.When you use Weight with
LinearLayout
. you must addweightSum
inLinearLayout
and according to orientation of yourLinearLayout
you must setting0dp
for Width/Height to allLinearLayout
`s Children viewsExample :
If The orientation of
Linearlayout
isVertical
, then Set Width of allLinearLayout
`s Children views with0dp
If the orientation
Linearlayout
of ishorizontal
, then Set Height of allLinearLayout
`s Children views with0dp
.也许将两个按钮的layout_width属性设置为“fill_parent”就可以了。
我刚刚测试了这段代码,它可以在模拟器中运行:
请务必将两个按钮上的layout_width设置为“fill_parent”。
Perhaps setting both of the buttons layout_width properties to "fill_parent" will do the trick.
I just tested this code and it works in the emulator:
Be sure to set layout_width to "fill_parent" on both buttons.
以下是代码中的更改(以粗体标记):
由于您的 LinearLayout 的方向为水平,因此您需要将宽度仅保留为 0dp。 以使用权重朝那个方向。 (如果您的方向是垂直的,您的高度将仅为 0dp)。
由于有 2 个视图,并且您为两个视图放置了
android:layout_weight="1"
,这意味着它将在水平方向(或按宽度)平均划分两个视图。Below are the changes (Marked in BOLD) in your code:
Since your LinearLayout has orientation as horizontal, therefore you will need to keep your width only as 0dp. for using weights in that direction . (If your orientation was vertical, you would have kept your height only 0dp).
Since there are 2 views and you have placed
android:layout_weight="1"
for both the views, it means it will divide the two views equally in horizontal direction (or by width).在上面的 XML 中,将线性布局的
android:layout_weight
设置为2
:android:layout_weight="2"
In the above XML, set the
android:layout_weight
of the linear layout as2
:android:layout_weight="2"
另外,您需要为
LinerLayout
的子视图 [Buttonviews] 添加此android:layout_width="0dp"
Plus you need to add this
android:layout_width="0dp"
for children views [Button views] ofLinerLayout
你必须这样写它为我工作
You have to write like this its Working for me
这是您问题的完美答案
This is perfect answer of your problem
将
wrap_content
替换为fill_parent
。Substitute
wrap_content
withfill_parent
.这里有一些例子
希望您有所帮助!
Here is some examples
Hope you helpful!