Moj*_*iiz 639 android android-widget android-layout
我不明白如何使用这个属性.谁能告诉我更多关于它的事情?
Flo*_*Flo 842
有了layout_weight你可以指定多个视图之间的大小比.例如,你有一个MapView和一个table应该向地图显示一些额外信息.地图应使用屏幕的3/4,表格应使用屏幕的1/4.然后你会设置layout_weight的map3和layout_weight的table1.
要使其工作,您还必须将高度或宽度(取决于您的方向)设置为0px.
ArK*_*ArK 245
简而言之,layout_weight指定布局中要分配给View的多少额外空间.
LinearLayout支持为各个孩子分配权重.此属性为视图指定"重要性"值,并允许它展开以填充父视图中的任何剩余空间.视图的默认权重为零.
一般来说,公式是:
分配给孩子的空间=(孩子的个人体重)/(线性布局中每个孩子的体重总和)
如果有三个文本框,其中两个声明权重为1,而第三个没有权重(0),则剩余空间分配如下:
第一个文本框= 1 /(1 + 1 + 0)
第二个文本框= 1 /(1 + 1 + 0)
第3个文本框= 0 /(1 + 1 + 0)
假设我们在水平行中有一个文本标签和两个文本编辑元素.标签没有layout_weight指定,因此它占用了渲染所需的最小空间.如果layout_weight两个文本编辑元素中的每一个都设置为1,则父布局中的剩余宽度将在它们之间平均分配(因为我们声称它们同样重要).
计算:
第一个标签= 0 /(0 + 1 + 1)
第二个文本框= 1 /(0 + 1 + 1)
第3个文本框= 1 /(0 + 1 + 1)
相反,如果第一个文本框的a layout_weight为1,第二个文本框的a layout_weight为2,则剩余空间的三分之一将被赋予第一个,而三分之二将被赋予第二个(因为我们声称第二个)一个更重要).
计算:
第一个标签= 0 /(0 + 1 + 2)
第二个文本框= 1 /(0 + 1 + 2)
第3个文本框= 2 /(0 + 1 + 2)
roe*_*tzi 69
添加到其他答案,使这个工作最重要的是将布局宽度(或高度)设置为0px
android:layout_width="0px"
Run Code Online (Sandbox Code Playgroud)
否则你会看到垃圾
Sur*_*gch 36
如果有多个视图跨越a LinearLayout,layout_weight则为每个视图提供一个比例大小.具有更大layout_weight价值的视图"更重",因此它获得更大的空间.
这是一个让事情更清晰的图像.
术语布局权重与数学中加权平均的概念有关.就像在大学课堂上,作业价值30%,出勤率10%,期中价值20%,决赛价值40%.这些部分的分数在加权后会给出总分.

布局重量也是一样的.的Views在水平LinearLayout可各自占用的总宽度的某个百分比.(或垂直高度的百分比LinearLayout.)
在LinearLayout您使用会是这个样子:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- list of subviews -->
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
请注意,您必须使用layout_width="match_parent"的LinearLayout.如果你使用wrap_content,那么它将无法正常工作.另请注意,layout_weight对于RelativeLayouts中的视图不起作用(有关此问题的SO答案,请参阅此处和此处).
横向的每个视图LinearLayout看起来像这样:
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
Run Code Online (Sandbox Code Playgroud)
请注意,您需要layout_width="0dp"与layout_weight="1".一起使用.忘记这会导致许多新用户出现问题.(请参阅此文章,了解通过不将宽度设置为0可获得的不同结果.)如果您的视图是垂直的, LinearLayout那么您layout_height="0dp"当然会使用.
在Button上面的示例中,我将权重设置为1,但您可以使用任何数字.重要的只是总数.您可以在我发布的第一张图像中的三行按钮中看到,数字都不同,但由于比例相同,因此每行的加权宽度不会改变.有些人喜欢使用总和为1的十进制数,这样在复杂的布局中,每个部分的重量都很清楚.
最后一点说明.如果您有许多使用的嵌套布局layout_weight,则可能对性能不利.
以下是顶部图像的xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android:layout_weight="
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<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="2"
android:text="2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android:layout_weight="
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10"
android:text="10" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="20" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10"
android:text="10" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android:layout_weight="
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text=".25" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".50"
android:text=".50" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text=".25" />
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Tas*_*iwa 29
layout_weight告诉Android如何分发你的Views LinearLayout.Android然后首先计算所有View具有指定权重的s 所需的总比例,并View根据它所指定的屏幕的所需部分放置每个比例.在以下示例中,Android将所述TextView■找一个layout_weight的0(这是默认值)和EditText■找一个layout_weight的2每个,而Button具有的重量1.所以Android分配"足够"的空间来显示tvUsername,tvPassword然后将屏幕宽度的剩余部分分成5个相等的部分,其中两个分配给etUsername,两个分配到etPassword最后一部分bLogin:
<LinearLayout android:orientation="horizontal" ...>
<TextView android:id="@+id/tvUsername"
android:text="Username"
android:layout_width="wrap_content" ... />
<EditText android:id="@+id/etUsername"
android:layout_width="0dp"
android:layout_weight="2" ... />
<TextView android:id="@+id/tvPassword"
android:text="Password"
android:layout_width="wrap_content" />
<EditText android:id="@+id/etPassword"
android:layout_width="0dp"
android:layout_weight="2" ... />
<Button android:id="@+id/bLogin"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Login"... />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
看起来像:
和

Geo*_*yen 15
这样想,会更简单
如果您有3个按钮,它们的权重相应为1,3,1,它将像HTML中的表格一样工作
为该线提供5个部分:按钮1的1个部分,按钮2的3个部分和按钮1的1个部分
看待,
And*_*ess 10
对我来说最好的解释之一是这个(来自Android教程,寻找第7步):
layout_weight在LinearLayouts中用于为布局中的视图指定"重要性".所有视图的默认layout_weight都为零,这意味着它们只占用屏幕上需要显示的空间.根据每个View的layout_weight的值及其与当前布局中为此View元素和其他View元素指定的总layout_weight的比率,分配高于零的值将拆分父视图中的剩余可用空间.
举个例子:假设我们在水平行中有一个文本标签和两个文本编辑元素.标签没有指定layout_weight,因此它占用了渲染所需的最小空间.如果两个文本编辑元素中每个文本编辑元素的layout_weight设置为1,则父布局中的剩余宽度将在它们之间平均分配(因为我们声称它们同样重要).如果第一个的layout_weight为1,而第二个的layout_weight为2,则剩余空间的三分之一将被赋予第一个,而三分之二将被赋予第二个(因为我们声称第二个更重要).
http://developer.android.com/guide/topics/ui/layout-objects.html#linearlayout
layout_weight定义控件必须分别为其他控件获取多少空间.
对于额外的?
对于vertical方向,不要忘记设置height为 0dp
android:layout_height="0dp"
Run Code Online (Sandbox Code Playgroud)
对于horizontal方向,不要忘记设置width为 0dp
android:layout_width="0dp"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
332417 次 |
| 最近记录: |