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 @   星锋  阅读(499)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示