android-UI组件实例大全(十二)------ScrollView滚动视图

本文介绍了如何使用ScrollView和HorizontalScrollView来实现垂直和水平滚动效果。通过XML布局代码示例展示了这两种视图的基本用法,并提供了在Java代码中设置初始滚动位置的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ScrollView滚动视图:

用于给普通组件添加一个垂直滚动条,最多包含一个组件,ScrollView与Swing中的JScrollPanel类似

甚至不能称为容器,只是为其他容器添加滚动条

水平方向的滚动条要使用:HorizontalScrollView


实例:

实现ScrollView垂直滚动和HorizontalScrollView水平滚动的视图

只需要在main.xml文件中创建

代码:

main.xml

[html]   view plain copy print ?
  1. <span style="font-family:Comic Sans MS;"><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent">  
  4. <HorizontalScrollView   
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="wrap_content">  
  7. <LinearLayout   
  8.     android:id="@+id/LinearLayout1"  
  9.     android:layout_width="wrap_content"  
  10.     android:layout_height="wrap_content"  
  11.     android:orientation="vertical" >  
  12.   
  13.     <TextView  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:textSize="60sp"  
  17.         android:text="@string/hello_world" />  
  18.     <TextView  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:textSize="60sp"  
  22.         android:text="@string/hello_world" />  
  23.     <TextView  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:textSize="60sp"  
  27.         android:text="@string/hello_world" />  
  28.     <TextView  
  29.         android:layout_width="wrap_content"  
  30.         android:layout_height="wrap_content"  
  31.         android:textSize="60sp"  
  32.         android:text="@string/hello_world" />  
  33.     <TextView  
  34.         android:layout_width="wrap_content"  
  35.         android:layout_height="wrap_content"  
  36.         android:textSize="60sp"  
  37.         android:text="@string/hello_world" />  
  38.     <TextView  
  39.         android:layout_width="wrap_content"  
  40.         android:layout_height="wrap_content"  
  41.         android:textSize="60sp"  
  42.         android:text="@string/hello_world" />  
  43.     <TextView  
  44.         android:layout_width="wrap_content"  
  45.         android:layout_height="wrap_content"  
  46.         android:textSize="60sp"  
  47.         android:text="@string/hello_world" />  
  48.     <TextView  
  49.         android:layout_width="wrap_content"  
  50.         android:layout_height="wrap_content"  
  51.         android:textSize="60sp"  
  52.         android:text="@string/hello_world" />  
  53.       
  54. </LinearLayout>  
  55. </HorizontalScrollView>  
  56. </ScrollView></span>  


代码分析:

就是用ScrollView和HorizontalScrollView包裹住我们的容器LinearLayout,然后就可以了


运行截图:


二.当然我们也可以在MainActivity中设置一些属性

实例:在java文件中设置scrollView的初始位置

代码:

[java]   view plain copy print ?
  1. <span style="font-family:Comic Sans MS;">private ScrollView scroll;  
  2.     private HorizontalScrollView hscroll;  
  3.     private LinearLayout line;  
  4.       
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.           
  10.         scroll = (ScrollView) findViewById(R.id.scroll);  
  11.         hscroll = (HorizontalScrollView) findViewById(R.id.hscroll);  
  12.         line = (LinearLayout) findViewById(R.id.LinearLayout1);  
  13.           
  14.         scroll.scrollTo(0,line.getBottom());  
  15.         hscroll.scrollTo(line.getWidth(), 0);  
  16.     }</span>  

这里的话我们利用.scrollTo方法去设置滚动条的起始位置,参数分别为x,y轴坐标

运行结果如上图,这里略过...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值