学习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
相关推荐
人工智能的苟富贵4 分钟前
Android Debug Bridge(ADB)完全指南
android·adb
Miqiuha10 分钟前
lock_guard和unique_lock学习总结
java·数据库·学习
一 乐1 小时前
学籍管理平台|在线学籍管理平台系统|基于Springboot+VUE的在线学籍管理平台系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习
加油,旭杏2 小时前
【中间件学习】fastCG介绍和使用
学习·nginx·fastcgi
limengshi1383922 小时前
通信工程学习:什么是TFTP简单文件传输协议
网络·网络协议·学习·信息与通信
GFCGUO3 小时前
ubuntu18.04运行OpenPCDet出现的问题
linux·python·学习·ubuntu·conda·pip
丝丝不是土豆丝4 小时前
学习 CSS 新的属性 conic-gradient 实现环形进度条
学习
S hh4 小时前
【Linux】进程地址空间
java·linux·运维·服务器·学习
小雨cc5566ru5 小时前
uniapp+Android面向网络学习的时间管理工具软件 微信小程序
android·微信小程序·uni-app
wusam5 小时前
螺蛳壳里做道场:老破机搭建的私人数据中心---Centos下Docker学习04(环境准备)
学习·docker·centos