理解 Android XML layout_weight 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3736663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems understanding Android XML layout_weight
提问by Dennis Winter
I really tried to, but I can't understand how Android interprets layout_weight
setting...
我真的尝试过,但我无法理解 Android 如何解释layout_weight
设置...
What I'm trying to achieve is
我想要实现的是
- a header on top with a fixed height
- an input area at the bottom containing an EditText and a Button
- a content part in the middle that's taking all what's left of space
- 顶部具有固定高度的标题
- 底部的输入区域包含一个 EditText 和一个 Button
- 中间的内容部分占用了所有剩余空间
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.
打字时,我想将 EditText 增加到特定高度,并在输入的文本超过可用高度时开始滚动。这样做我需要周围的环境LinearLayout
与 EditText 一起成长。
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
. :(
如果我为内部定义一个特定的高度,LinearLayout
它就不会增长。如果我不这样做,无论我尝试使用什么,内部布局都会占用所有空间而不是 ScrollView layout_weight
。:(
My current XML looks like that:
我当前的 XML 看起来像这样:
<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>
Any tips are greatly appreciated!
非常感谢任何提示!
回答by Cheryl Simon
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.
layout_weight
用于确定在各种小部件获得他们想要的所有空间后,Android 如何划分任何剩余空间。
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:
假设您连续有 3 个小部件,每个小部件需要 10 像素,但您有 50 像素的空间。以下是 layout_weights 的一些示例以及每个小部件将声明的像素数:
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
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:
您可以将权重视为可用空间的百分比。它会将所有值相加,然后根据需要分配部分。因此,如果有帮助,您可以使用加起来为 100 的权重来计算百分比:
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.
而且,您希望它上面的小部件接管所有额外的空间。
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.
不幸的是,在 Android 中实际上不可能指定最大大小。我认为你能做的最好的事情是给 ScrollView 一些固定大小,layout_weight = 1 并告诉底部 LinearLayout 为 wrap_content。这会(我认为)导致 LinearLayout 扩展,直到它不能再扩展,因为 ScrollView 已经占用了空间。
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...
然而,挑战是为 ScrollView 指定一个固定大小,这在所有不同的屏幕尺寸和分辨率下都有意义。您最终可能不得不为不同的分辨率创建不同的布局,这可能不值得。就个人而言,我只是给 editText 一个固定大小......
回答by javaxian
I've had a similar problem (a footer with the refresh timestamp sticking to the bottom of the screen and a ListView/ScrollView taking up all the remainig space) and after many tries it worked for me using the following layout:
我遇到了类似的问题(刷新时间戳粘在屏幕底部的页脚和一个 ListView/ScrollView 占用了所有剩余空间),经过多次尝试后,它使用以下布局对我有用:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/refresh_date_text"
android:layout_alignParentTop="true"
android:orientation="vertical"
>
<ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#FFFFFF"
android:layout_weight="1" android:divider="@drawable/list_divider"
android:dividerHeight="1dp" android:drawSelectorOnTop="false" />
<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center_horizontal"
android:paddingTop="10dp"
android:text="The list is empty."/>
</LinearLayout>
<TextView android:id="@+id/refresh_date_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:background="#f0f0f0"
android:textStyle="italic"
android:textSize="12dp"
/>
</RelativeLayout>
No clipping the ListView when with a scroller, perfect alignment in all orientations, properly showing the "list is empty" text.
使用滚动条时不会剪切 ListView,在所有方向上完美对齐,正确显示“列表为空”文本。
回答by Kevin Coppock
I believe you would be better off with a RelativeLayout. Add your header, and set it to layout_alignParentTop="true"
. Then add your EditText and Button, set that area to layout_alignParentBottom="true"
. Then, add the center area, set the width and height to fill_parent
, and set layout_above="{the id of the EditText/Button layout}"
, and layout_below="{the id of the header layout}"
.
我相信使用RelativeLayout 会更好。添加您的标题,并将其设置为layout_alignParentTop="true"
. 然后添加您的 EditText 和 Button,将该区域设置为layout_alignParentBottom="true"
. 然后,添加中心区域,将宽度和高度fill_parent
设置为,并设置layout_above="{the id of the EditText/Button layout}"
、 和layout_below="{the id of the header layout}"
。
I don't have Eclipse at work, otherwise I would test it for you, but that should work.
我没有 Eclipse 在工作,否则我会为你测试它,但这应该可行。
回答by vikram
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
>
<TextView
android:text=" TextView "
android:textColor="#ffffff"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
/>
<EditText
android:text=" EditText "
android:textSize="15sp"
android:layout_width="250dp"
android:layout_weight="1"
android:layout_height="0dp"
/>
<Button
android:text=" button "
android:textColor="#000000"
android:textSize="15sp"
android:gravity="center_vertical|center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
This will help
这将有助于