繁体   English   中英

如何使相对布局垂直/水平滚动?

[英]How to make relative layout scroll vertically/horizontally?

我使用相对布局制作了一个布局,事实证明它很长。 我想改用ListView,但是在ListView上执行此操作相当复杂。

我知道,只要屏幕溢出屏幕边缘,Android OS就能以某种方式上下左右左右平移屏幕。

到目前为止,这是我当前正在做的事情,我将相对布局插入ScrollView内,希望它会向下滚动,因为UI内容比屏幕长。 我希望它像在许多应用程序中一样向下滚动。

这是我的代码:

    <?xml version="1.0" encoding="utf-8"?>

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingStart="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:fillViewport="true"
    tools:context=".TaskActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/textinputlayout_task_location"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="24dp"
            android:paddingEnd="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin">

            <EditText
                android:id="@+id/edittext_location"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:singleLine="true"
                android:padding="8dp"
                android:hint="@string/edittext_task_location_hint"/>

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/textinputlayout_task_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/textinputlayout_task_location"
            android:paddingTop="16dp"
            android:paddingEnd="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin">

            <EditText
                android:id="@+id/edittext_task"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:singleLine="true"
                android:textSize="32sp"
                android:padding="8dp"
                android:hint="@string/edittext_task_title_hint"/>

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/textinputlayout_task_description"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/textinputlayout_task_title"
            android:layout_above="@+id/linearlayout_task_buttons"
            android:paddingEnd="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin">

            <EditText
                android:id="@+id/edittext_todo"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="top"
                android:hint="@string/edittext_task_content_hint"/>

        </android.support.design.widget.TextInputLayout>

        <LinearLayout
            android:id="@id/linearlayout_task_buttons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/simpledividerview_schedule"
            android:orientation="horizontal">

            <ImageButton
                android:id="@+id/imagebutton_insert_note"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/ic_content_add_black"
                android:contentDescription="@string/content_description_choose_note"
                style="?android:attr/borderlessButtonStyle" />

            <ImageButton
                android:id="@+id/imagebutton_expand_note"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/ic_open_in_new_black_24dp"
                android:contentDescription="@string/content_description_expand_note"
                style="?android:attr/borderlessButtonStyle" />

        </LinearLayout>

        <com.neonwarge.android.notifire.utils.view.SimpleDividerView
            android:id="@id/simpledividerview_schedule"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/linearlayout_task_start_date"
            app:showHeaderText="true"
            app:headerText="@string/header_schedule"/>

        <LinearLayout
            android:id="@id/linearlayout_task_start_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/linearlayout_task_end_date"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            android:layout_marginEnd="@dimen/activity_horizontal_margin"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textview_start_date"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="8"
                android:text="@string/textview_start_date"
                style="@android:style/Widget.Holo.Light.Spinner" />

            <android.support.v7.widget.SwitchCompat
                android:id="@+id/switchcompat_alarm_on_start"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textOff="@string/switchcompat_off"
                android:textOn="@string/switchcompat_on"
                android:background="@android:color/transparent" />

        </LinearLayout>

        <LinearLayout
            android:id="@id/linearlayout_task_end_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/simpledividerview_reminder"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            android:layout_marginEnd="@dimen/activity_horizontal_margin"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textview_end_date"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="8"
                android:text="@string/textview_end_date"
                style="@android:style/Widget.Holo.Light.Spinner" />

            <android.support.v7.widget.SwitchCompat
                android:id="@+id/switchcompat_alarm_on_end"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textOff="@string/switchcompat_off"
                android:textOn="@string/switchcompat_on"
                android:background="@android:color/transparent" />

        </LinearLayout>

        <com.neonwarge.android.notifire.utils.view.SimpleDividerView
            android:id="@id/simpledividerview_reminder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/linearlayout_task_alarm_before_start_date"
            app:showHeaderText="true"
            app:headerText="@string/header_reminder"/>

        <LinearLayout
            android:id="@+id/linearlayout_task_alarm_before_start_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/linearlayout_task_before_end_date"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            android:layout_marginEnd="@dimen/activity_horizontal_margin"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textview_before_start_date"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="8"
                android:text="@string/textview_alarm_before_start_date"
                style="@android:style/Widget.Holo.Light.Spinner"/>

            <android.support.v7.widget.SwitchCompat
                android:id="@+id/switchcompat_alarm_before_on_start"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textOff="@string/switchcompat_off"
                android:textOn="@string/switchcompat_on"
                android:background="@android:color/transparent" />

        </LinearLayout>

        <LinearLayout
            android:id="@id/linearlayout_task_before_end_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/simpledividerview_settings"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            android:layout_marginEnd="@dimen/activity_horizontal_margin"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textview_before_end_date"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="8"
                android:text="@string/textview_alarm_before_end_date"
                style="@android:style/Widget.Holo.Light.Spinner" />

            <android.support.v7.widget.SwitchCompat
                android:id="@+id/switchcompat_before_alarm_on_end"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textOff="@string/switchcompat_off"
                android:textOn="@string/switchcompat_on"
                android:background="@android:color/transparent" />

        </LinearLayout>

        <com.neonwarge.android.notifire.utils.view.SimpleDividerView
            android:id="@id/simpledividerview_settings"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/linearlayout_task_show_notification"
            app:showHeaderText="true"
            app:headerText="@string/header_reminder"/>

        <LinearLayout
            android:id="@id/linearlayout_task_show_notification"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            android:layout_marginEnd="@dimen/activity_horizontal_margin"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:orientation="horizontal">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="8">

                <TextView
                    android:id="@+id/textview_task_show_notifications"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="8"
                    android:text="@string/textview_task_alarm_sound" />

            </android.support.design.widget.TextInputLayout>

            <android.support.v7.widget.SwitchCompat
                android:id="@+id/switchcompat_task_show_notifications"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textOff="@string/switchcompat_off"
                android:textOn="@string/switchcompat_on"
                android:background="@android:color/transparent" />

        </LinearLayout>


    </RelativeLayout>
</ScrollView>

问题是,如果我在手机上查看它。 压缩布局以适合屏幕。 我不喜欢这样 我必须能够滚动。

我无法使相对布局的内容很长,无法在scrollview中滚动。

根据代码,可能是什么问题?

谢谢!

您的问题是这样的:

android:layout_alignParentBottom="true"

您要告诉操作系统将该视图锚定在屏幕底部,并且一些视图位于该视图上方。 这迫使所有内容都适合在单个屏幕中。

您需要用具有垂直方向的LinearLayout替换RelativeLayout,然后按从上到下的顺序进行布局。

我认为您的ScrollView不应该是主布局(虽然不能100%确定),您的主布局应该是Layout(RelativeLayout或LinearLAyout,具有match_parent宽度和wrap_content高度),并且ScrollView嵌套在其中。

至于水平滚动( Source ):

ScrollView仅支持垂直滚动。 对于水平滚动,请使用Horizo​​ntalScrollView

您应该为您的RelativeLayout设置android:layout_height="wrap_content"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM