android scrollbar的使用demo
时间: 2023-07-11 18:22:51 浏览: 62
以下是一个简单的 Android ScrollView 中添加 scrollbar 的示例代码:
1. 首先,在布局文件中添加 ScrollView 控件:
```
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 添加需要滚动的控件 -->
</ScrollView>
```
2. 然后,在代码中为 ScrollView 设置 scrollbar:
```
ScrollView scrollView = findViewById(R.id.scroll_view);
scrollView.setVerticalScrollBarEnabled(true);
scrollView.setHorizontalScrollBarEnabled(true);
```
通过设置 `setVerticalScrollBarEnabled(true)` 和 `setHorizontalScrollBarEnabled(true)` 来启用垂直和水平 scrollbar。
3. 可以通过设置 ScrollView 的 `scrollbarThumbVertical` 和 `scrollbarTrackVertical` 属性来自定义 scrollbar 的样式:
```
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarThumbVertical="@drawable/custom_scrollbar_thumb"
android:scrollbarTrackVertical="@drawable/custom_scrollbar_track">
<!-- 添加需要滚动的控件 -->
</ScrollView>
```
其中,`custom_scrollbar_thumb` 和 `custom_scrollbar_track` 是自定义的 scrollbar 样式。
相关推荐
![qml](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)