Android:垂直 LinearLayout 中的 ScrollView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12130797/
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
Android: ScrollView in vertical LinearLayout
提问by kakko76
I'm making an app for Android device and I'm trying to have a ScrollView inside a LinearLayout but when I try to do this the ScrollView take all the space and elements who are after the ScrollView in the LinearLayout disapear.
我正在为 Android 设备制作一个应用程序,我正在尝试在 LinearLayout 中包含一个 ScrollView,但是当我尝试这样做时,ScrollView 会占用 LinearLayout 中 ScrollView 之后的所有空间和元素。
For example:
If the ScrollView is not "full":
If the ScrollView is "full":
例如:
如果 ScrollView 不是“full”:
如果 ScrollView 是“full”:
As you can see the button disapear...
Here is the code of this Activity:
如您所见,按钮消失了……
这是此活动的代码:
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_marginLeft="50dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/linearlayoutbackground" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nom_des_joueurs"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginBottom="30dp" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/llPlayersName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/okPlayersName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/ok"
android:background="@drawable/backgroundbutton"
android:layout_marginTop="30dp" />
</LinearLayout>
After I add elements in the LinearLayout who are in the ScrollView.
Any solution?
Thanks.
在我在 ScrollView 中的 LinearLayout 添加元素之后。
有什么解决办法吗?
谢谢。
回答by type-a1pha
Try the following:
请尝试以下操作:
android:layout_height="0dp"
android:layout_weight="1"
This tells your ScrollView to take all the remaining space not occupied by the other elements.
这会告诉您的 ScrollView 使用其他元素未占用的所有剩余空间。
回答by roiberg
you had android:layout_height="wrap_content"
as your height. that means that the height is as high as the content inside it. if you dont want it to take the whole place you can give it weight (like the answer above) or you can set your hight with dp. for example:
你有android:layout_height="wrap_content"
你的身高。这意味着高度与其内部的内容一样高。如果你不想让它占据整个地方,你可以给它权重(就像上面的答案)或者你可以用 dp 设置你的高度。例如:
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_marginLeft="50dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/linearlayoutbackground"
android:weightSum="1.5" >
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight = "0.2"
android:text="@string/nom_des_joueurs"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginBottom="30dp" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight = "1">
<LinearLayout
android:id="@+id/llPlayersName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/okPlayersName"
android:layout_width="match_parent"
android:layout_height="0"
android:layout_weight = "0.3">
android:text="@string/ok"
android:background="@drawable/backgroundbutton"
android:layout_marginTop="30dp" />
as you can see i gave the linear layout a "sum" and all of his children are given weight. is it clear? is it what you needed?
正如你所看到的,我给了线性布局一个“总和”,他所有的孩子都被赋予了权重。清楚吗?这是你需要的吗?