如何使布局水平和垂直滚动?

Leo*_*onz 2 android android-layout android-studio

我知道如何通过将布局放入 ScrollView 来使布局可垂直滚动。但是我怎样才能让它也可以横向滚动呢?

Har*_*iya 5

ScrollViewHorizontalScrollViewview hierarchy用户可以垂直或水平滚动的布局容器,允许它大于物理显示。AScrollView/HorizontalScrollView是 a FrameLayout,这意味着您应该在其中放置一个包含要滚动的全部内容的子项;这个孩子本身可能是一个具有复杂对象层次结构的布局管理器。

这是 xml :

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

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

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

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Inside 1st HorizontalScrollView" />

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

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button A1" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button A2" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button A3" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button A4" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button A5" />
            </LinearLayout>
        </LinearLayout>
    </HorizontalScrollView>

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

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

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Inside 2nd HorizontalScrollView" />

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

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button B1" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button B2" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button B3" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button B4" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button B5" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button B6" />
            </LinearLayout>
        </LinearLayout>
    </HorizontalScrollView>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Inside ScrollView" />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Button C" />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Button D" />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Button E" />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Button F" />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Button G" />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Button H" />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Button I" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请访问:http : //android-coding.blogspot.in/2011/01/scrollview-and-horizo​​ntalscrollview.html

更新 :

使用此 xml 滚动vertical以及horizontal.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:scrollbars="vertical">

        <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="320px" android:layout_height="fill_parent">
   
            

        </HorizontalScrollView>

    </ScrollView>
Run Code Online (Sandbox Code Playgroud)