Android 中的线性布局和权重 [英] Linear Layout and weight in Android

查看:27
本文介绍了Android 中的线性布局和权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是在 Android 文档中读到这个有趣的权重值.现在我想第一次尝试它,但它根本不起作用.

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.

推荐答案

您没有设置 layout_weight 属性.您的代码读取 weight="1" 并且它应该读取 android:layout_weight="1".

You are not setting the layout_weight property. Your code reads weight="1" and it should read android:layout_weight="1".

这篇关于Android 中的线性布局和权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆