我总是在 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>
应该创建两个水平对齐并平均共享空间的按钮。问题是两个按钮不会增长以填充空间。
我希望按钮能够增长并填满整条线。如果两个按钮都设置为匹配父级,则仅显示第一个按钮并填充整行。
3件事要记住:
将孩子的 android:layout_width 设置为“0dp”
设置父级的 android:weightSum (编辑:正如 Jason Moore 所注意到的,此属性是可选的,因为默认情况下它设置为子级的 layout_weight 总和)
按比例设置每个孩子的 android:layout_weight (eg weightSum="5", 三个孩子: layout_weight="1", layout_weight="3", layout_weight="1")
例子:
<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>
结果:
https://i.stack.imgur.com/yew2A.png
您没有设置 layout_weight
属性。您的代码读取 weight="1"
,它应该读取 android:layout_weight="1"
。
这是android:layout_weight
。权重只能在 LinearLayout
中使用。如果 linearlayout 的方向是 Vertical,则使用 android:layout_height="0dp"
,如果方向是水平,则使用 android:layout_width = "0dp"
。它会完美地工作。
此图像总结了线性布局。
https://i.stack.imgur.com/s5K9I.png
您可以点击此链接以获取有关该主题的更多信息。 Just Maths - Views, View Groups and Layouts
线性布局的视频教程:宽度、高度和重量
Android Linear Layout Tutorial
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)
示例(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)
如果第一个文本框的 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)
在按钮的宽度字段中,将 wrap-content
替换为 0dp
。
使用视图的 layout_weight 属性。
android:layout_width="0dp"
这就是您的代码的样子:
<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>
layout_weight 用于将剩余空间按比例分配。在这种情况下,两个按钮采用“0dp”宽度。因此,剩余空间将在它们之间按 1:1 的比例划分,即空间将在 Button View 之间平均分配。
喜欢@Manoj Seelan的答案
将 android:layout_weight
替换为 android:weight
。
当您将 Weight 与 LinearLayout
一起使用时。您必须在 LinearLayout
中添加 weightSum
,并且根据 LinearLayout
的方向,您必须将 0dp
设置为所有 LinearLayout
的子视图的宽度/高度
例子 :
如果 Linearlayout
的方向是 Vertical
,则使用 0dp
设置所有 LinearLayout
的子视图的宽度
<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>
如果的方向 Linearlayout
是 horizontal
,则使用 0dp
设置所有 LinearLayout
的子视图的高度。
<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>
也许将两个按钮的 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>
请务必在两个按钮上将 layout_width 设置为“fill_parent”。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/logonFormButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:orientation="horizontal">
<Button
android:id="@+id/logonFormBTLogon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/logon"
android:layout_weight="0.5" />
<Button
android:id="@+id/logonFormBTCancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/cancel"
android:layout_weight="0.5" />
</LinearLayout>
以下是代码中的更改(以粗体标记):
<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" //changes made here
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_weight="1" /> //changes made here
<Button
android:text="Not this time"
android:id="@+id/cancel"
android:layout_width="0dp" //changes made here
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_weight="1" /> //changes made here
</LinearLayout>
由于您的 LinearLayout 的方向为水平,因此您只需将宽度保持为 0dp。在那个方向上使用权重。 (如果你的方向是垂直的,你的高度只会保持 0dp)。
由于有 2 个视图并且您为两个视图都放置了 android:layout_weight="1"
,这意味着它将在水平方向(或按宽度)等分两个视图。
在上述 XML 中,将线性布局的 android:layout_weight
设置为 2
:android:layout_weight="2"
此外,您需要为 LinerLayout
的子视图 [按钮视图] 添加此 android:layout_width="0dp"
你必须这样写它为我工作
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:text="Register"
android:id="@+id/register"
android:layout_width="wrap_content"
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="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_weight="1" />
<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="2"
android:text="Button 1" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Button 2" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button 3" />
</LinearLayout>
这是您问题的完美答案
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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>
将 wrap_content
替换为 fill_parent
。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#008">
<RelativeLayout
android:id="@+id/paneltamrin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
>
<Button
android:id="@+id/BtnT1"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:drawableTop="@android:drawable/ic_menu_edit"
android:drawablePadding="6dp"
android:padding="15dp"
android:text="AndroidDhina"
android:textColor="#000"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/paneltamrin2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
>
<Button
android:layout_width="wrap_content"
android:layout_height="150dp"
android:drawableTop="@android:drawable/ic_menu_edit"
android:drawablePadding="6dp"
android:padding="15dp"
android:text="AndroidDhina"
android:textColor="#000"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
https://i.stack.imgur.com/YCaJb.png
这里有一些例子
等重的水平方向
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="1"
android:text="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="1"
android:text="2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="1"
android:text="3" />
</LinearLayout>
https://i.stack.imgur.com/ZTEmr.png
权重不等的水平方向
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="1"
android:text="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="2"
android:text="2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="1"
android:text="3" />
</LinearLayout>
https://i.stack.imgur.com/ejDNS.png
等重垂直方向
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_margin="8dp"
android:layout_weight="1"
android:text="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_margin="8dp"
android:layout_weight="1"
android:text="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_margin="8dp"
android:layout_weight="1"
android:text="3" />
</LinearLayout>
https://i.stack.imgur.com/kN9UJ.png
希望对您有所帮助!
addView(button, new LinearLayout.LayoutParams(0, height, 1));
等布局参数一起使用,即使您使用正确的宽度和权重值对布局进行膨胀也是如此。