HorizontalScrollView基本用法

HorizontalScrollView是水平滚动标签。垂直滚动动是 ScrollView标签。

这里有一个需要注意的是这两个标签都只能包含一个子节点,如果写多个子节点系统会直接闪退掉。

这是使用这个标签需要注意的。

直接看测试源码

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

    <HorizontalScrollView android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:background="#2196F3"
        android:scrollbars="none"
        android:overScrollMode="never"
        android:fadeScrollbars="false"
        >
        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:orientation="horizontal"
            >
            <View android:layout_width="300dp"
                android:layout_height="match_parent"
                android:background="#F44336"
                />

            <View android:layout_width="300dp"
                android:layout_height="match_parent"
                android:background="#00BCD4"
                />
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

这样是实现了一个简单的水平滚动

去掉默认的滚动条还有两端拖动的装饰:

可以直接在XML源码里面设置

android:scrollbars="none"
android:overScrollMode="never"

即可。

 

也可以通过JAVA来设置,看一下java源码部分:

HorizontalScrollView horizontalScrollView = findViewById(R.id.hscroll);
horizontalScrollView.setOverScrollMode(ScrollView.OVER_SCROLL_NEVER);
horizontalScrollView.setHorizontalScrollBarEnabled(false);

实现的效果跟在XML里面设置是一样的。

 

posted @ 2022-07-23 18:02  星锋  阅读(442)  评论(0编辑  收藏  举报