[英]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僅支持垂直滾動。 對於水平滾動,請使用HorizontalScrollView
您應該為您的RelativeLayout設置android:layout_height="wrap_content"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.