具有二维滚动的RelativeLayout

发布于 2024-12-29 17:55:28 字数 7652 浏览 1 评论 0原文

我正在尝试使用许多小部件创建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>

我的活动看起来像这样:

Activity

一切都很好,该活动垂直滚动。

但后来我尝试像这样添加horizo​​ntalScrollView:

<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>

标记中没有错误,但活动现在看起来像这样: Activity

所有 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:

Activity

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:
Activity

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心头的小情儿 2025-01-05 17:55:28

maybe it will helpful for you, there is a Link which describes how to make two dimensional scroll with ScrollView and HorizntalScrollView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文