具有二维滚动的RelativeLayout
我正在尝试使用许多小部件创建RelativeLayout,并且希望能够垂直和水平滚动它。 首先,我让它仅使用该 xml 垂直滚动:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:orientation="vertical" >
<ScrollView
android:id="@+id/layoutPostWriteScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:layout_marginRight="5dp"
android:fitsSystemWindows="true"
android:gravity="center" >
<TextView
android:id="@+id/layoutPostWriteCtrlLabelTheme"
style="@style/News.Caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Тема" />
<EditText
android:id="@+id/layoutPostWriteCtrlTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/layoutPostWriteCtrlLabelTheme"
android:inputType="text" />
<LinearLayout
android:id="@+id/layoutPostWriteEmbeddedMediaLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layoutPostWriteCtrlTheme"
android:gravity="left"
android:orientation="vertical" />
<Button
android:id="@+id/layoutPostWriteAddMediaButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layoutPostWriteEmbeddedMediaLayout"
android:onClick="onAddMediaClick"
android:text="+ Добавить медиафайл" />
<TextView
android:id="@+id/layoutPostWriteCtrlLabelContent"
style="@style/News.Caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layoutPostWriteAddMediaButton"
android:text="Содержание" />
<EditText
android:id="@+id/layoutPostWriteCtrlContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layoutPostWriteCtrlLabelContent"
android:gravity="top"
android:inputType="textMultiLine"
android:lines="10"
android:scrollbars="vertical"
android:singleLine="false" />
<Button
android:id="@+id/layoutPostWriteCtrlSave"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_below="@id/layoutPostWriteCtrlContent"
android:background="@drawable/btn_save"
android:onClick="onClickSendPost" />
</RelativeLayout>
</ScrollView>
我的活动看起来像这样:
一切都很好,该活动垂直滚动。
但后来我尝试像这样添加horizontalScrollView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:orientation="vertical" >
<ScrollView
android:id="@+id/layoutPostWriteScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:fitsSystemWindows="true">
<HorizontalScrollView
android:id="@+id/layoutPostWriteHScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="fill"
android:fillViewport="true"
android:fitsSystemWindows="true" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:layout_marginRight="5dp"
android:fitsSystemWindows="true"
android:gravity="center" >
<TextView
android:id="@+id/layoutPostWriteCtrlLabelTheme"
style="@style/News.Caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Тема" />
<EditText
android:id="@+id/layoutPostWriteCtrlTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/layoutPostWriteCtrlLabelTheme"
android:inputType="text" />
<LinearLayout
android:id="@+id/layoutPostWriteEmbeddedMediaLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layoutPostWriteCtrlTheme"
android:gravity="left"
android:orientation="vertical" />
<Button
android:id="@+id/layoutPostWriteAddMediaButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layoutPostWriteEmbeddedMediaLayout"
android:onClick="onAddMediaClick"
android:text="+ Добавить медиафайл" />
<TextView
android:id="@+id/layoutPostWriteCtrlLabelContent"
style="@style/News.Caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layoutPostWriteAddMediaButton"
android:text="Содержание" />
<EditText
android:id="@+id/layoutPostWriteCtrlContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layoutPostWriteCtrlLabelContent"
android:gravity="top"
android:inputType="textMultiLine"
android:lines="10"
android:scrollbars="vertical"
android:singleLine="false" />
<Button
android:id="@+id/layoutPostWriteCtrlSave"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_below="@id/layoutPostWriteCtrlContent"
android:background="@drawable/btn_save"
android:onClick="onClickSendPost" />
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
标记中没有错误,但活动现在看起来像这样:
所有 EditText 和整个relativelayout 都缩小了。如何使活动看起来像第一个图像,但垂直和水平滚动?我尝试了多种layout_width和layout_height的组合,但似乎不起作用。
I'm trying to create RelativeLayout with many widgets and I want to be able to scroll it vertically and horizontally.
At first, I made it to scroll only vertically with that xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:orientation="vertical" >
<ScrollView
android:id="@+id/layoutPostWriteScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:layout_marginRight="5dp"
android:fitsSystemWindows="true"
android:gravity="center" >
<TextView
android:id="@+id/layoutPostWriteCtrlLabelTheme"
style="@style/News.Caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Тема" />
<EditText
android:id="@+id/layoutPostWriteCtrlTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/layoutPostWriteCtrlLabelTheme"
android:inputType="text" />
<LinearLayout
android:id="@+id/layoutPostWriteEmbeddedMediaLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layoutPostWriteCtrlTheme"
android:gravity="left"
android:orientation="vertical" />
<Button
android:id="@+id/layoutPostWriteAddMediaButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layoutPostWriteEmbeddedMediaLayout"
android:onClick="onAddMediaClick"
android:text="+ Добавить медиафайл" />
<TextView
android:id="@+id/layoutPostWriteCtrlLabelContent"
style="@style/News.Caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layoutPostWriteAddMediaButton"
android:text="Содержание" />
<EditText
android:id="@+id/layoutPostWriteCtrlContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layoutPostWriteCtrlLabelContent"
android:gravity="top"
android:inputType="textMultiLine"
android:lines="10"
android:scrollbars="vertical"
android:singleLine="false" />
<Button
android:id="@+id/layoutPostWriteCtrlSave"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_below="@id/layoutPostWriteCtrlContent"
android:background="@drawable/btn_save"
android:onClick="onClickSendPost" />
</RelativeLayout>
</ScrollView>
My activity looks like that:
Everything's allright and that activity scrolls vertically.
But then I tried to add horizontalScrollView like that:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:orientation="vertical" >
<ScrollView
android:id="@+id/layoutPostWriteScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:fitsSystemWindows="true">
<HorizontalScrollView
android:id="@+id/layoutPostWriteHScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="fill"
android:fillViewport="true"
android:fitsSystemWindows="true" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:layout_marginRight="5dp"
android:fitsSystemWindows="true"
android:gravity="center" >
<TextView
android:id="@+id/layoutPostWriteCtrlLabelTheme"
style="@style/News.Caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Тема" />
<EditText
android:id="@+id/layoutPostWriteCtrlTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/layoutPostWriteCtrlLabelTheme"
android:inputType="text" />
<LinearLayout
android:id="@+id/layoutPostWriteEmbeddedMediaLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layoutPostWriteCtrlTheme"
android:gravity="left"
android:orientation="vertical" />
<Button
android:id="@+id/layoutPostWriteAddMediaButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layoutPostWriteEmbeddedMediaLayout"
android:onClick="onAddMediaClick"
android:text="+ Добавить медиафайл" />
<TextView
android:id="@+id/layoutPostWriteCtrlLabelContent"
style="@style/News.Caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layoutPostWriteAddMediaButton"
android:text="Содержание" />
<EditText
android:id="@+id/layoutPostWriteCtrlContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layoutPostWriteCtrlLabelContent"
android:gravity="top"
android:inputType="textMultiLine"
android:lines="10"
android:scrollbars="vertical"
android:singleLine="false" />
<Button
android:id="@+id/layoutPostWriteCtrlSave"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_below="@id/layoutPostWriteCtrlContent"
android:background="@drawable/btn_save"
android:onClick="onClickSendPost" />
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
There is no errors in markup but activity now looks like that:
All EditTexts and whole RelativeLayout shrinked. How can I make activity to look like at the first image, but scroll vertical and horizontal? I tried many combinations of layout_width and layout_height, but it seems to not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许这对你有帮助,有一个链接 描述了如何使用 ScrollView 和 HorizntalScrollView 进行二维滚动。
maybe it will helpful for you, there is a Link which describes how to make two dimensional scroll with ScrollView and HorizntalScrollView.