如何使用Android中的ScrollView垂直滚动来向左和向右滑动

时间:2017-03-30 15:34:26

标签: android

我在scrollView中有一些布局。这些布局必须在左右方向上可以移动,但它会与滚动视图产生一些冲突。如何使用Android中的ScrollView同时滑动和滚动。我不想使用ViewPager。

请帮助我,因为我对android很新鲜。

先谢谢。

2 个答案:

答案 0 :(得分:1)

在您的父HorizontalScrollView内使用左右滚动的ScrollView

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="your_width" android:layout_height="your_height" >

 <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="your_width" android:layout_height="your_height">

</HorizontalScrollView>

</ScrollView>

答案 1 :(得分:1)

您可以使用HorizontalScrollView水平滚动child views,并将此HorizontalScrollView放在ScrollView内。同时将LinearLayout Vertical方向添加为ScrollView的直接子项。

这是一个工作示例。试试这个:

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

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

        <!-- First row -->
        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

                <Button
                    android:layout_width="200dp"
                    android:layout_height="200dp"
                    android:text="One"/>

                <Button
                    android:layout_width="200dp"
                    android:layout_height="200dp"
                    android:text="Two"/>

                <Button
                    android:layout_width="200dp"
                    android:layout_height="200dp"
                    android:text="Three"/>

            </LinearLayout>
        </HorizontalScrollView>

        <!-- Second row -->
        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

                <Button
                    android:layout_width="200dp"
                    android:layout_height="200dp"
                    android:text="One"/>

                <Button
                    android:layout_width="200dp"
                    android:layout_height="200dp"
                    android:text="Two"/>

                <Button
                    android:layout_width="200dp"
                    android:layout_height="200dp"
                    android:text="Three"/>

            </LinearLayout>
        </HorizontalScrollView>

        <!-- Third row -->
        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

                <Button
                    android:layout_width="200dp"
                    android:layout_height="200dp"
                    android:text="One"/>

                <Button
                    android:layout_width="200dp"
                    android:layout_height="200dp"
                    android:text="Two"/>

                <Button
                    android:layout_width="200dp"
                    android:layout_height="200dp"
                    android:text="Three"/>

            </LinearLayout>
        </HorizontalScrollView>

    </LinearLayout>
</ScrollView>

<强>输出:

enter image description here

希望这会有所帮助〜

相关问题