How to use HorizontalScrollView?

2019-08-19 03:33发布

I want my app to be able to scroll horizontally so it can slide between pages. But when I use this feature on android sdk it won't let me make the xml layout fill parent i.e. it won't let me have a full screen to include my buttons and objects.

Any idea on how can I fix this or what is an alternate method?

Here's the code:

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="348dp"
        android:layout_height="559dp" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="341dp"
            android:layout_marginLeft="146dp"
            android:src="@drawable/ic_launcher" />

    </RelativeLayout>


</HorizontalScrollView>

1条回答
戒情不戒烟
2楼-- · 2019-08-19 04:12

Change the relative layout to LinearLayout. Similar to a vertical ScrollView, it makes use of a LinearLayout.

Here is a sample layout I have in one of my projects:

<HorizontalScrollView
            android:id="@+id/horizontalScrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="none" >

            <LinearLayout
                android:id="@+id/LinearLayout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal" >

                <Button
                     android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/class_tab" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/exam" />

                <Button
                    android:id="@+id/button_more"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/more" />

            </LinearLayout>
        </HorizontalScrollView>
查看更多
登录 后发表回答