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

时间:2016-04-13 15:30:25

标签: android android-layout android-fragments

我使用相对布局制作了一个布局,结果很长。我想创建一个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>

问题在于,如果我在手机上查看它。布局被压扁以适应屏幕。我不喜欢这个。我必须能够滚动。

我不能让我的相对布局很长的内容在滚动视图中滚动。

基于代码,可能是什么问题?

谢谢!

3 个答案:

答案 0 :(得分:1)

你的问题是:

android:layout_alignParentBottom="true"

您告诉操作系统将此视图锚定到屏幕底部,并且在此视图之上放置了一些视图。这迫使一切都适合一个屏幕。

您需要将VerticalLayout替换为具有垂直方向的LinearLayout,然后从上到下按顺序排列。

答案 1 :(得分:0)

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

关于水平滚动(Source):

  

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

答案 2 :(得分:0)

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

相关问题