Android开发-Android常用组件-ScrollView滚动条
4.10 ScrollView(滚动条)
我们可以直接利用ScrollView给我们提供的:fullScroll()方法。
scrollView.fullScroll(ScrollView.FOCUS_DOWN); |
滚动到底部 |
scrollView.fullScroll(ScrollView.FOCUS_UP); |
滚动到顶部 |
设置滚动速度:
继承ScrollView,然后重写一个 public void fling (int velocityY)的方法:
@Override public void fling(int velocityY) { super.fling(velocityY / 2); //速度变为原来的一半 }
tips: ScrollView控件中只能包含一个View或一个ViewGroup。
例:
scrollview_layout.xml:
<?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/btn_down" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="滚动到底部"/> <Button android:id="@+id/btn_up" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="滚动到顶部"/> <ScrollView android:id="@+id/scroll_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" android:layout_weight="1"> <TextView android:id="@+id/txt_show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/> </ScrollView> </LinearLayout>
ScrollViewActivity.java:
package com.example.myapplication; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ScrollView; import android.widget.TextView; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; public class ScrollViewActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_down; private Button btn_up; private ScrollView scrollView; private TextView txt_show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.scrollview_layout); bindViews(); } private void bindViews(){ btn_down = (Button) findViewById(R.id.btn_down); btn_up = (Button) findViewById(R.id.btn_up); scrollView = (ScrollView) findViewById(R.id.scroll_view); txt_show = (TextView) findViewById(R.id.txt_show); btn_down.setOnClickListener(this); btn_up.setOnClickListener(this); StringBuilder sb = new StringBuilder(); for (int i = 1; i <= 100 ; i++) { sb.append("我是一条文本内容 * " + i + "\n"); } txt_show.setText(sb.toString()); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_down: scrollView.fullScroll(ScrollView.FOCUS_DOWN); break; case R.id.btn_up: scrollView.fullScroll(ScrollView.FOCUS_UP); break; } } }
AndroidManifest.java:
启动运行:
分类:
安卓基础
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性