使RelativeLayout滚动

时间:2018-10-24 20:51:23

标签: android xml

我希望RelativeLayout在Landscape中可滚动。 我尝试这样做:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".RelativeActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/headline_icon" />

    <TextView
        android:id="@+id/relative_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/layout_uebersicht" />

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relative_textview"
        android:layout_centerHorizontal="true"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/relativebutton_linearlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="onButtonLinearLayoutClick"
                android:text="@string/linear_layout" />

            <Button
                android:id="@+id/relativebutton_tablelayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="onButtonTableLayoutClick"
                android:text="@string/table_layout" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/relativebutton_framelayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="onButtonFrameLayoutClick"
                android:text="@string/frame_layout" />

            <Button
                android:id="@+id/relativebutton_gridlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="onButtonGridLayoutClick"
                android:text="@string/grid_layout" />
        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:src="@drawable/baseline_icon" />
</RelativeLayout>
</ScrollView>

dataframe documentation

我也尝试过在ScrollView和RelativeLayout之间放置一个简单的LinearLayout,并切换所有的layout_height和width值。这些都不行。

android:fillViewport="true"也不起作用。

But it messes everything up like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".RelativeActivity">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/headline_icon" />

<TextView
    android:id="@+id/relative_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="@string/layout_uebersicht" />

<LinearLayout
    android:id="@+id/ll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/relative_textview"
    android:layout_centerHorizontal="true"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/relativebutton_linearlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onButtonLinearLayoutClick"
            android:text="@string/linear_layout" />

        <Button
            android:id="@+id/relativebutton_tablelayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onButtonTableLayoutClick"
            android:text="@string/table_layout" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/relativebutton_framelayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onButtonFrameLayoutClick"
            android:text="@string/frame_layout" />

        <Button
            android:id="@+id/relativebutton_gridlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onButtonGridLayoutClick"
            android:text="@string/grid_layout" />
    </LinearLayout>
</LinearLayout>

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:src="@drawable/baseline_icon" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

创建一个HorizontalScrollView并在其中设计RelativeLayout

<HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="16dp">

            <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:src="@drawable/headline_icon" />

            <TextView
                    android:id="@+id/relative_textview"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="@string/layout_uebersicht" />

            <LinearLayout
                    android:id="@+id/ll"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/relative_textview"
                    android:layout_centerHorizontal="true"
                    android:orientation="vertical">

                <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                    <Button
                            android:id="@+id/relativebutton_linearlayout"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:onClick="onButtonLinearLayoutClick"
                            android:text="@string/linear_layout" />

                    <Button
                            android:id="@+id/relativebutton_tablelayout"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:onClick="onButtonTableLayoutClick"
                            android:text="@string/table_layout" />
                </LinearLayout>

                <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                    <Button
                            android:id="@+id/relativebutton_framelayout"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:onClick="onButtonFrameLayoutClick"
                            android:text="@string/frame_layout" />

                    <Button
                            android:id="@+id/relativebutton_gridlayout"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:onClick="onButtonGridLayoutClick"
                            android:text="@string/grid_layout" />
                </LinearLayout>
            </LinearLayout>

            <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:src="@drawable/baseline_icon" />
        </RelativeLayout>
    </HorizontalScrollView>

考虑您的RelativeLayout宽度应为wrap_content