新思想

ScrollView

ScrollView

 

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.xiesir.example13scrollview.MainActivity">

    <Button
        android:id="@+id/btnTop"
        android:text="到顶部"
        android:onClick="click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btnBottom"
        android:text="到底部"
        android:onClick="click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ScrollView
        android:id="@+id/sv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/txtshow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />

    </ScrollView>
</LinearLayout>

MainActivity.java:

package com.xiesir.example13scrollview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView txtshow = (TextView) findViewById(R.id.txtshow);

        StringBuilder sb = new StringBuilder();
        sb.append("fullScroll(ScrollView.FOCUS_DOWN):滚动到底部\n" +
                "fullScroll(ScrollView.FOCUS_UP):滚动到顶部\n" +
                "scrollbarThumbVertical / scrollbarThumbHorizontal:设置滚动的滑块图片\n\n");
        for (int i = 1; i <= 80; i++)
            sb.append("学号 " + i + "\n");
        txtshow.setText(sb.toString());

    }

    public void click(View v) {
        ScrollView sv = (ScrollView) findViewById(R.id.sv);
        switch (v.getId()) {
            case R.id.btnBottom:
                sv.fullScroll(ScrollView.FOCUS_DOWN);
                break;
            case R.id.btnTop:
                sv.fullScroll(ScrollView.FOCUS_UP);
                break;
        }
    }

}

源程序下载

参考:

posted on 2016-05-17 01:45  新思想  阅读(262)  评论(0)    收藏  举报

导航