问题的了解Android的XML layout_weight [英] Problems understanding Android XML layout_weight

查看:85
本文介绍了问题的了解Android的XML layout_weight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的尽力了,但我无法理解的Andr​​oid跨$ P $点 layout_weight 设置...

I really tried to, but I can't understand how Android interprets layout_weight setting...

我试图做到的,是

  • 有一个固定的高度上顶部的标题
  • 包含一个EditText和一个按钮
  • 输入区域底部
  • 在内容部分,中间多数民众赞成采取一切还剩下什么空间

在打字,我想成长的EditText到一个特定的高度,并开始滚动,如果输入的文本超出了可用的高度。这样做,我需要周围的的LinearLayout 与EditText上共同成长。

When typing I'd like to grow the EditText to a specific height and to start scrolling if the text entered exceeds the available height. Doing this I need the surrounding LinearLayout to grow together with the EditText.

如果我定义一个特定的高度内的LinearLayout 也不会增长。如果我不这样做,内部布局采用的所有空间,而不是滚动型,不管什么我尝试使用 layout_weight 。 :(

If I define a specific height for the inner LinearLayout it won't grow. If I don't, the inner layout takes ALL the space instead of the ScrollView, no matter what I try using layout_weight. :(

我目前的XML看起来像:

My current XML looks like that:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="I'm a header" />
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="bottom">
        <LinearLayout
            android:id="@+id/itemContainer"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </ScrollView>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:padding="2dp"
        android:gravity="bottom"
        android:isScrollContainer="true"
        android:background="@drawable/steel" >
        <EditText
            android:id="@+id/edittext01"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:scrollbars="vertical"
            android:fadingEdge="vertical"
            android:fadingEdgeLength="60dp"
            android:maxHeight="40dp"
            android:textSize="15sp" />
        <Button
            android:id="@+id/button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:layout_gravity="right"
            android:text="Send"
            android:textStyle="bold"
            android:textSize="15sp" />
    </LinearLayout>
</LinearLayout>

任何提示都大大AP preciated!

Any tips are greatly appreciated!

推荐答案

layout_weight 用于确定机器人如何划分的任何遗留下来的空间后,各部件得到所有他们希望的空间。

layout_weight is used to determine how Android divides up any left over space after the various widgets get all of the space they want.

所以说,你有3个部件在一排,每个要10个像素,但你有50个像素的空间。下面是layout_weights的几个例子和像素数每个插件会声称:

So say you have 3 widgets in a row, each of which want 10 pixels, but you have 50 pixels worth of space. Here are a few examples of layout_weights and the number of pixels each widget will claim:

widget1: weight = 1, 30px
widget2: weight = 0, 10px
widget3: weight = 0, 10px

widget1: weight = 1, 20px
widget2: weight = 1, 20px
widget3: weight = 0, 10px

widget1: weight = 1, 16.5px
widget2: weight = 1, 16.5px
widget3: weight = 1, 16.5px

您可以把重量为可用空间的百分比。这将增加了所有的值,然后给出了部分合适。因此,如果它可以帮助你可以使用加起来100权重做百分比:

You can think of the weight as a percentage of available space. It will add up all of the values, and then give out portions as appropriate. Thus, if it helps you can use weights that add up to 100 to do percentages:

widget1: weight = 0, 10px
widget2: weight = 25, 15px
widget3: weight = 75, 25px

如果我理解正确的话,你想要的底部部件,开始在一个特定的大小,然后根据需要扩展到一些最大尺寸,然后滚动。

If I understand correctly, you want the bottom widget to start at a particular size, then expand to some maximum size if necessary, and then scroll.

和,你想要的部件上面它接管所有的额外空间。

And, you want the widget above it to take over all of the extra space.

不幸的是在机器人它不是真的能够确定的最大尺寸。我认为你能做的只是给了滚动型一些固定的大小,以layout_weight = 1,并告诉底部的LinearLayout来WRAP_CONTENT最好的。这将(我认为)导致的LinearLayout扩大,直到它再也看不到,因为滚动型已经声称的空间。

Unfortunately in Android its not realy possible to specify a maximum size. I think the best you could do would be to give the ScrollView some fixed size, with a layout_weight = 1 and tell the bottom LinearLayout to wrap_content. This would (I think) cause the LinearLayout to expand until it can't anymore, because the ScrollView has already claimed the space.

不过,challange被指定一个固定大小的滚动型是有道理的,在所有不同的屏幕大小和分辨率。你可能最终不得不为不同的分辨率,这可能是不值得创建不同的布局。就个人而言,我只是给EDITTEXT一个固定大小...

However, the challange is specifying a fixed size for the ScrollView that makes sense at all different screen sizes and resolutions. You might end up having to create different layouts for the different resolutions, which is probably not worth it. Personally, I'd just give the editText a fixed size...

这篇关于问题的了解Android的XML layout_weight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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