Android-相对布局中ScrollView中的LinearLayout
我的布局有点问题。 我制作了RelativeLayout,其中放置了两个LinearLayout(1 和2),并在它们之间放置了带有LinearLayout 的ScrollView。接下来,我将 ScrollView 设置为放置在 LinearLayout2 下方和 LinearLayout1 上方。但是 ScrollView 内部的 LinearLayout 没有设置为它的父级。
这是代码,也许我犯了一个错误:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dip"
android:layout_alignParentTop="true"
android:id="@+id/lin1"
android:layout_weight="1">
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1" android:id="@+id/lin3"
android:layout_height="60dip">
</LinearLayout>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="horizontal"
android:layout_below="@id/lin1"
android:layout_above="@id/lin3" android:id="@+id/ScrollView">
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:id="@+id/lin2"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal">
<Button android:text="@string/button1"
android:id="@+id/button1"
android:layout_gravity="center"
android:layout_width="280dip"
android:layout_height="60dip">
</Button>
<Button android:text="@string/button2"
android:id="@+id/button2"
android:layout_gravity="center"
android:layout_width="280dip"
android:layout_height="60dip">
</Button>
<Button android:text="@string/button3"
android:id="@+id/button3"
android:layout_gravity="center"
android:layout_width="280dip"
android:layout_height="60dip">
</Button>
<Button android:text="@string/button4"
android:id="@+id/button4"
android:layout_gravity="center"
android:layout_width="280dip"
android:layout_height="60dip">
</Button>
</LinearLayout>
</ScrollView>
有什么想法,为什么 ScrollView 内部的 LinearLayout 比 ScrollView 更大?
编辑: 到目前为止,我注意到当我设置layout_gravity =“center_vertical”时,ScrollView内部的LinearLayout会移到外部。看起来设置中心是指主布局(RelativeLayout)。
I have a little problem with layouts.
I have made RelativeLayout in which I put two LinearLayouts (1 and 2) and between them ScrollView with LinearLayout. Next I set that ScrollView to be placed under linearLayout2 and above linearLayout1. But LinearLayout inside that ScrollView isn't set as it's parent.
Here's the code, maybe I've made a mistake:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dip"
android:layout_alignParentTop="true"
android:id="@+id/lin1"
android:layout_weight="1">
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1" android:id="@+id/lin3"
android:layout_height="60dip">
</LinearLayout>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="horizontal"
android:layout_below="@id/lin1"
android:layout_above="@id/lin3" android:id="@+id/ScrollView">
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:id="@+id/lin2"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal">
<Button android:text="@string/button1"
android:id="@+id/button1"
android:layout_gravity="center"
android:layout_width="280dip"
android:layout_height="60dip">
</Button>
<Button android:text="@string/button2"
android:id="@+id/button2"
android:layout_gravity="center"
android:layout_width="280dip"
android:layout_height="60dip">
</Button>
<Button android:text="@string/button3"
android:id="@+id/button3"
android:layout_gravity="center"
android:layout_width="280dip"
android:layout_height="60dip">
</Button>
<Button android:text="@string/button4"
android:id="@+id/button4"
android:layout_gravity="center"
android:layout_width="280dip"
android:layout_height="60dip">
</Button>
</LinearLayout>
</ScrollView>
Any ideas, why LinearLayout inside of ScrollView is bigger than the ScrollView?
EDIT:
So far I've noticed that LinearLayout inside of ScrollView moves outside when I set layout_gravity="center_vertical". It looks like setting center refers to main layout (RelativeLayout).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这个代码我认为这是最好的解决方案
try this code i think this is the best sollution
好的,我成功了。
我所要做的就是将 ScrollView 内部的 LinearLayout 设置为
ScrollView 到
最后,我将该 ScrollView 放入新的 LinearLayout 中,参数如下:
因此 ScrollView 及其子布局设置按钮水平中心和外部LinearLayout 将它们设置为垂直居中。 :-)
Ok, I've made it.
All I had to do is setting LinearLayout that is inside of ScrollView to
The ScrollView to
And at the end I've put that ScrollView into new LinearLayout with parameters as following:
So the ScrollView and it's child Layout are setting buttons center horizontaly and the outer LinearLayout is setting them center verticaly. :-)
尝试将
lin2
的layout_width
更改为wrap_content
而不是fill_parent
。Try changing the
layout_width
oflin2
towrap_content
instead offill_parent
.