android基本控件学习-----ScrollView

ScrollView(滚动条)的讲解:

一、对于ScrollView滚动条还是很好理解的,共有两种水平和垂直,ScrollView和HorizontalScrollview,这个里面不知道该总结写什么,说说几个方法吧

scrollView.fullScroll(ScrollView.FOCUS.DOWN):回到低部

scrollView.fullScroll(ScrollView.FOCUS.UP):回到顶部

scrollView.setVerticalSrcollBarEnabled(false):隐藏滑块(好像设置了没什么用)

android:scrollbarThumbVertical:设置垂直滚动的图片(好像设置了没什么用)

android:scrollbarThumbHorizontal:设置水平滚动的图片(好像设置了没什么用)

android:srollbars = "none"隐藏滑块(好像设置了没什么用)

如果我们要控制滑块的滑动速度我们需要继承ScrollView复写public void fling (int velocityY)的方法,这个个人觉得单独总结ScrollView没什么说的,先就这些吧

二.简单使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="回到顶部"/>
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="回到底部"/>
   <ScrollView
       android:id="@+id/sv"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">
       <TextView
           android:id="@+id/tv1"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:scrollbarThumbVertical="@mipmap/ic_launcher"
           android:text="哈哈"/>
   </ScrollView>
</LinearLayout>

Java代码

package com.example.test3;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener{
    private  TextView textView;
    private ScrollView scrollView;
    private Button btn1;
    private Button btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.tv1);
        scrollView = (ScrollView) findViewById(R.id.sv);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        StringBuffer sb = new StringBuffer();
        for(int i = 0;i < 100;i++){
            sb.append("呵呵" + i + "\n");
        }
        textView.setText(sb.toString());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case  R.id.btn1:{
                scrollView.fullScroll(ScrollView.FOCUS_UP);
                break;
            }
            case R.id.btn2:{
                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                break;
            }
        }
    }
}

效果图:

 

posted @ 2016-01-11 19:03  会飞的一只狼  阅读(5067)  评论(0编辑  收藏  举报