android基础:android页面添加滚动条

这篇博客介绍了在Android应用中创建滚动效果的方法。首先,创建一个Android项目,然后在activity_main.xml布局文件中删除原有内容,插入ScrollView作为容器,并在其中添加多个TextView和一个Button。每个TextView显示相同的文本,用于演示滚动效果。运行项目,即可看到带有滚动条的页面。关键在于使用<ScrollView>标签来实现下拉滚动,注意其内部只能放置一个父视图。

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

1、建立一个android项目。

2、打开layout下的activity_main.xml文件,如果创建项目事修改了activity_main.xml文件就选择你修改的文件名。

3、删除activity_main.xml文件中内容。

4、将下列文件布置到activity_main.xml中。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
 android:layout_width="fill_parent"  
 android:layout_height="fill_parent"  
 android:fadingEdge="vertical">

<RelativeLayout
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"
    <!-- 需要上传一张图片 也可以去掉这行--> android:background="@drawable/back"
  >

    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="17dp"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="47dp"
        android:text="@string/hello_world" />
    
     <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView01"
        android:layout_below="@+id/TextView01"
        android:layout_marginTop="47dp"
        android:text="@string/hello_world" />
     
      <TextView
        android:id="@+id/TextView03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView02"
        android:layout_below="@+id/TextView02"
        android:layout_marginTop="47dp"
        android:text="@string/hello_world" />
      
       <TextView
        android:id="@+id/TextView04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView03"
        android:layout_below="@+id/TextView03"
        android:layout_marginTop="47dp"
        android:text="@string/hello_world" />
       
        <TextView
        android:id="@+id/TextView05"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView04"
        android:layout_below="@+id/TextView04"
        android:layout_marginTop="47dp"
        android:text="@string/hello_world" />
        
         <TextView
        android:id="@+id/TextView06"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView05"
        android:layout_below="@+id/TextView05"
        android:layout_marginTop="47dp"
        android:text="@string/hello_world" />
         
          <TextView
        android:id="@+id/TextView07"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView06"
        android:layout_below="@+id/TextView06"
        android:layout_marginTop="47dp"
        android:text="@string/hello_world" />
          
           <TextView
        android:id="@+id/TextView08"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView07"
        android:layout_below="@+id/TextView07"
        android:layout_marginTop="47dp"
        android:text="@string/hello_world" />
           
            <TextView
        android:id="@+id/TextView09"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView08"
        android:layout_below="@+id/TextView08"
        android:layout_marginTop="47dp"
        android:text="@string/hello_world" />
            
             <TextView
        android:id="@+id/TextView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView09"
        android:layout_below="@+id/TextView09"
        android:layout_marginTop="47dp"
        android:text="@string/hello_world" />

             <Button
                 android:id="@+id/button1"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignLeft="@+id/TextView10"
                 android:layout_below="@+id/TextView10"
                 android:layout_marginTop="47dp"
                 android:text="@string/hello_world"/>
      
</RelativeLayout>
</ScrollView>

5、运行你的项目就会出现下来效果。

总结:主要作用的是<ScrollView>这个标签他的左右是建立下拉效果,但要注意下拉效果他的位置只能放下最外层。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值