編輯:關於 Android 開發
ScrollView(滾動視圖)是 Android 中非常基礎的容器控件,主要用來承載超出螢幕範圍、需要滾動查看的內容。本文整理了它的常用方法與簡單用法。
ScrollView 共有水平與垂直兩種形式,分別是 ScrollView 與 HorizontalScrollView。以下列幾個常用方法:
scrollView.fullScroll(ScrollView.FOCUS_DOWN):捲動到底部;
scrollView.fullScroll(ScrollView.FOCUS_UP):回到頂部;
scrollView.setVerticalScrollBarEnabled(false):隱藏滑塊;
android:scrollbarThumbVertical:設定垂直滾動的圖示;
android:scrollbarThumbHorizontal:設定水平滾動的圖示;
android:scrollbars = "none":隱藏滑塊。
如果想要控制滑塊的滑動速度,可以繼承 ScrollView 並覆寫 public void fling(int velocityY) 方法來實現。
先在布局中放入兩個用來回到頂部/底部的按鈕,以及一個包著大量文字的 ScrollView。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://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; } } } }完成後即可看到點擊按鈕時頁面自動捲動到頂部或底部的效果。
Android 進階系列(四):Activity 生命週期與 Intent 的顯式、隱式用法
Android 的三大基礎元件——Activity、Service 與 BroadcastReceiver——都必須靠 Intent 才能啟動。前面幾篇我們已經反覆用到
ListView 添加頭布局與腳布局的完整實現
ListView 添加頭布局與腳布局的完整實現為 ListView 添加頭布局與腳布局這是之前跟隨喜馬拉雅教程做的一個小 Demo,貼出來供大家參考。當一個頁面存在多個接
Context 到底是什麼?從繼承體系到能力邊界一次講清楚
一、Context 是什麼 官方文件的說法是:關於應用程式環境的全域資訊介面。它是一個抽象類,實作由 Android 系統提供,讓你能存取應用程式專屬的資源與類別,並執行
Paint API 系列(8.3.13):Shader 圖像渲染五種子類全解
Android 基礎入門教程——8.3.13 Paint API 之 Shader(圖像渲染) 本節引言 最近因為工作和新職面試的事耽誤了更新,這裡說聲抱歉。今天下午