学习Android的第十二天

目录

[Android SeekBar:拖动条控件](#Android SeekBar:拖动条控件)

[SeekBar 属性](#SeekBar 属性)

[SeekBar 事件](#SeekBar 事件)

[SeekBar 定制](#SeekBar 定制)

范例:

参考文档

[Android RatingBar 星级评分条](#Android RatingBar 星级评分条)

[RatingBar 属性](#RatingBar 属性)

[RatingBar 样式](#RatingBar 样式)

[RatingBar 事件](#RatingBar 事件)

范例:

官方文档

[Android ScrollView 滚动视图](#Android ScrollView 滚动视图)

ScrollView

滚动到底部或顶部

设置滚动的滑块图片

隐藏滑块

设置滚动速度

官方文档


Android SeekBar:拖动条控件

SeekBar(拖动条)是Android中常用的用户界面控件之一,通常用于控制音乐播放器或视频播放器的音量、播放进度等。作为 ProgressBar 的子类,SeekBar 继承了 ProgressBar 的所有属性,并且还具有一些自己特有的属性和方法。

SeekBar 属性

SeekBar 控件的简单属性包括:

  • android:max="100":设置滑动条的最大值为 100。
  • android:progress="60":设置滑动条的当前值为 60。这表示默认情况下滑动条显示的进度位置。
  • android:secondaryProgress="70":设置二级滑动条的进度为 70。某些场景下,SeekBar 可以展示两种进度,比如下载进度和缓冲进度,此属性用于设置缓冲进度。
  • android:thumb="@mipmap/sb_icon":设置滑块的 drawable 为指定的图像资源 sb_icon。滑块是用户可以拖动的部分,该属性允许你自定义滑块的外观。

SeekBar 事件

SeekBar 内置了 SeekBar.OnSeekBarChangeListener 事件监听器,其中包含三个方法:

  • onProgressChanged(SeekBar seekBar, int progress, boolean fromUser):当拖动条的进度发生改变时触发。参数 seekBar 是触发该事件的 SeekBar 对象,progress 是当前的进度值,fromUser 表示进度是否是由用户手动操作触发的。
  • onStartTrackingTouch(SeekBar seekBar):当用户开始按住 SeekBar 拖动时触发。参数 seekBar 是触发该事件的 SeekBar 对象。
  • onStopTrackingTouch(SeekBar seekBar):当用户停止拖动 SeekBar 时触发。参数 seekBar 是触发该事件的 SeekBar 对象。

SeekBar 定制

SeekBar提供了两个属性来定制它的外观:

1、android:progressDrawable:用于设置 SeekBar 轨道的 Drawable。通过设置这个属性,你可以自定义 SeekBar 的轨道样式,可以是颜色、图片等。例如:

XML 复制代码
<SeekBar
    android:id="@+id/seekBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="0"
    android:progressDrawable="@drawable/custom_progress_drawable" />

2、android:thumb:用于设置 SeekBar 拖动条的 Drawable。这个属性用于自定义 SeekBar 的拖动按钮的外观。你可以使用一个图片作为滑块,也可以使用自定义的形状。例如:

XML 复制代码
<SeekBar
    android:id="@+id/seekBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="0"
    android:thumb="@drawable/custom_thumb" />

范例:

1、在 XML 布局文件中定义 SeekBar

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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:gravity="center">

        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:progress="60"
            android:thumb="@drawable/seekbar_thumb_pressed" />

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="当前音量: 60/100" />

</LinearLayout>

2、修改 MainActivity.java 给 SeekBar 添加一个 SeekBar.OnSeekBarChangeListener

java 复制代码
package com.example.myapplication;

import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

        SeekBar seekBar = findViewById(R.id.seekBar);
        TextView  text1 = findViewById(R.id.text);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // 当进度发生变化时调用
                Log.d("SeekBar", "当前值:" + progress);
                text1.setText("当前音量:"+ progress+"/100" );
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // 当用户开始拖动SeekBar时调用
                Log.d("SeekBar", "开始拖动");
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // 当用户停止拖动SeekBar时调用
                Log.d("SeekBar", "停止拖动");
            }
        });
    }
}

参考文档

官方文档: SeekBar

Android RatingBar 星级评分条

Android 中的 RatingBar 是一个用户界面控件,用于允许用户通过选择星级评分来表示对某个项目或内容的喜好程度。它显示一行星形图标,用户可以在图标中选择一个评分,通常是从1到5颗星。

RatingBar 属性

在 Android 中,RatingBar 控件有几个常用的属性:

  • android:isIndicator:该属性用于指示 RatingBar 是否仅用于显示评分,而用户无法更改。默认值为 false,即用户可以点击选择评分。如果将其设置为 true,则 RatingBar 将变为只读模式,用户不能更改评分。
  • android:numStars:该属性指定了 RatingBar 显示的星星数量。它必须是一个整数值,表示星星的数量。例如,如果设置为 5,则 RatingBar 将显示 5 颗星。
  • android:rating:这是 RatingBar 的默认评分值。它必须是一个浮点数值,表示 RatingBar 初始化时显示的评分。默认值为 0.0。
  • android:stepSize:该属性指定评分每次增加的值。它必须是一个浮点数值,表示每次用户点击时评分增加的量。例如,如果设置为 0.5,则每次点击评分增加 0.5。默认值为 0.5。

RatingBar 样式

在 Android 中,除了默认的 RatingBar 样式外,还提供了两个其他样式,但是它们并不常用,因为它们可能在某些设备上显示不一致,或者看起来不够美观。这两个样式是:

  • style="?android:attr/ratingBarStyleSmall":这个样式适用于小尺寸的 RatingBar,通常用于较小的控件或者嵌入到其他控件中。它可能在不同设备上的显示效果会有所不同。
  • style="?android:attr/ratingBarStyleIndicator":这个样式用于指示性的 RatingBar,通常用于显示当前评分的状态,而不允许用户进行交互。这种样式的 RatingBar 是只读的,用户不能改变评分。

RatingBar 事件

RatingBar 提供了 OnRatingBarChangeListener 事件监听器,用于监听评分变化事件。这个监听器包含一个方法:

  • onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser):当用户改变评分时触发该方法。参数 ratingBar 是触发事件的 RatingBar 对象,rating 是当前的评分值,fromUser 表示评分是否是由用户触发的。在这个方法中,可以响应用户的评分变化,并进行相应的处理。

范例:

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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:gravity="center">

        <RatingBar
            android:id="@+id/ratingBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:isIndicator="false"
            android:numStars="5"
            android:rating="3.5"
            android:stepSize="0.5" />

        <TextView
            android:id="@+id/tv_rating"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="当前评分:3.5" />

</LinearLayout>
java 复制代码
package com.example.myapplication;

import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

        RatingBar ratingBar = findViewById(R.id.ratingBar);
        TextView ratingTextView = findViewById(R.id.tv_rating);

        // 设置 RatingBar 的当前评分值
        float currentRating = ratingBar.getRating();

        // 显示当前评分值
        ratingTextView.setText("当前评分值: " + currentRating);

        // 设置 RatingBar 的评分变化监听器
        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                // 当评分发生变化时更新评分文本
                ratingTextView.setText("当前评分值: " + rating);
            }
        });
    }
}

官方文档

  1. RatingBar

Android ScrollView 滚动视图

在 Android 中,ScrollView 是一个常用的滚动视图容器,用于在屏幕空间不足以容纳所有内容时,允许用户在垂直方向上滚动内容。ScrollView 可以包含一个子视图,这个子视图可以是单个组件,也可以是一个布局包含的复杂层次结构。

ScrollView 主要用于处理以下情况:

  1. 当内容超出屏幕范围时,允许用户通过滚动操作来查看隐藏部分的内容。
  2. 当需要在垂直方向上滚动显示内容时,例如长列表或大段文字等。

ScrollView

例子:

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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:gravity="center">

        <ScrollView
            android:id="@+id/scrollview"
            android:layout_width="match_parent"
            android:layout_height="500dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/extralongtext" />
        </ScrollView>
</LinearLayout>

滚动到底部或顶部

方法 fullScroll(int direction) 可以用于将 ScrollView 滚动到底部或顶部。这个方法接受一个参数 direction,指定滚动的方向。常量 ScrollView.FOCUS_DOWN 表示滚动到底部,而常量 ScrollView.FOCUS_UP 则表示滚动到顶部。

java 复制代码
// 滚动到底部
scrollView.fullScroll(ScrollView.FOCUS_DOWN);

// 滚动到顶部
scrollView.fullScroll(ScrollView.FOCUS_UP);

例子:

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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:gravity="center">

        <Button
            android:id="@+id/btn_down"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="滚动到底部" />
        <ScrollView
            android:id="@+id/scrollview"
            android:layout_width="match_parent"
            android:layout_height="500dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/extralongtext" />
        </ScrollView>
        <Button
            android:id="@+id/btn_up"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="滚动到顶部" />
</LinearLayout>
java 复制代码
package com.example.myapplication;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

        // 获取按钮和 ScrollView 对象
        Button btnDown = findViewById(R.id.btn_down);
        Button btnUp = findViewById(R.id.btn_up);
        ScrollView scrollView = findViewById(R.id.scrollview);

        // 滚动到底部按钮点击事件
        btnDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });

        // 滚动到顶部按钮点击事件
        btnUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollView.fullScroll(ScrollView.FOCUS_UP);
            }
        });
    }
}

设置滚动的滑块图片

在 Android 中,可以使用 android:scrollbarThumbVertical 和 android:scrollbarThumbHorizontal 属性来设置 ScrollView 或 HorizontalScrollView 的滚动条的滑块图片。

隐藏滑块

要隐藏 ScrollView 或 HorizontalScrollView 的滚动条滑块,可以使用两种方法:

方法一:通过 XML 属性设置

可以在 XML 布局文件中直接设置 android:scrollbars 属性为 "none" 来隐藏滚动条滑块。

XML 复制代码
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none">

    <!-- ScrollView 内容 -->

</ScrollView>

方法二:通过 Java 代码设置

也可以在 Java 代码中调用 setVerticalScrollBarEnabled(false) 或 setHorizontalScrollBarEnabled(false) 方法来隐藏滚动条滑块。

java 复制代码
ScrollView scrollView = findViewById(R.id.scrollview);
scrollView.setVerticalScrollBarEnabled(false);

或者对于 HorizontalScrollView:

XML 复制代码
HorizontalScrollView horizontalScrollView = findViewById(R.id.horizontal_scrollview);
horizontalScrollView.setHorizontalScrollBarEnabled(false);

设置滚动速度

虽然 ScrollView 并没有直接提供设置滚动速度的方法,但是可以通过继承 ScrollView 并重写其 fling(int velocityY) 方法来实现。在重写的方法中,可以调整滚动速度。

XML 复制代码
@Override
public void fling(int velocityY) {
    super.fling(velocityY / 2);    //速度变为原来的一半
}

官方文档

  1. Android ScrollView
相关推荐
19岁开始学习1 小时前
Go学习-入门
开发语言·学习·golang
私人珍藏库1 小时前
[Android] APK提取器(1.3.7)版本
android
m0_748232641 小时前
mysql的主从配置
android·mysql·adb
秋长愁1 小时前
Android监听应用前台的实现方案解析
android
ianozo1 小时前
CTF 代码学习日记 PHP
java·学习·php
大G哥2 小时前
用DeepSeek来帮助学习three.js加载3D太极模形
开发语言·前端·javascript·学习·ecmascript
胖虎12 小时前
2025 新版Android Studio创建Java语言项目
android·java·android studio·创建java项目
没有不重的名么2 小时前
MATLAB基础学习相关知识
数据结构·学习·matlab
omage2 小时前
cornerstone3D学习笔记-MPR
笔记·学习·vtk·dicom·mpr
虾球xz3 小时前
游戏引擎学习第114天
学习·游戏引擎