Android控件默认风格解析之SeekBar

本文涉及的产品
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
简介: 在我们开发的时候常常需要更改原生控件的默认效果,有时候某些控件改起来挺费劲的,比如SeekBar的背景与其ProgressBar的进度粗细或者thumb居中现实与否如果弄错,都是个大麻烦,我曾经就为thumb的居中显示问题浪费了很多很多的时间,后来以别的笨拙的办法解决了,现在重新回来看,决定下决心整一下,看看到底是怎么回事。

在我们开发的时候常常需要更改原生控件的默认效果,有时候某些控件改起来挺费劲的,比如SeekBar的背景与其ProgressBar的进度粗细或者thumb居中现实与否如果弄错,都是个大麻烦,我曾经就为thumb的居中显示问题浪费了很多很多的时间,后来以别的笨拙的办法解决了,现在重新回来看,决定下决心整一下,看看到底是怎么回事。

我们知道,当我们在写一个xml布局的时候,只需要简单的为这个控件指定一个高宽便可以看到它的效果,就像这样:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:progress="30"/>
</RelativeLayout>

预览效果图:


我们可以看到这个控件本身是带有一种风格的,那这个风格在哪里被定义了呢?我们一起来找找:

我们当然需要先去SeeBar的类里面找找有什么关键的信息:

我们在SeeBar的重载构造方法中看到一条有用的信息:

    public SeekBar(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.seekBarStyle);
    }

原来奥秘就在com.android.internal.R.attr.seekBarStyle的里面,我们找找去,seekBarStyle位于platform_frameworks_base-master\core\res\res\values\themes.xml文件中,在该文件中可以发现它的定义:

        <item name="seekBarStyle">@style/Widget.SeekBar</item>

看来它是调用了位于style下的Widget.SeekBar风格,我们再去找找:

    <style name="Widget.SeekBar">
        <item name="indeterminateOnly">false</item>
        <item name="progressDrawable">@drawable/progress_horizontal</item>
        <item name="indeterminateDrawable">@drawable/progress_horizontal</item>
        <item name="minHeight">20dip</item>
        <item name="maxHeight">20dip</item>
        <item name="thumb">@drawable/seek_thumb</item>
        <item name="thumbOffset">8dip</item>
        <item name="focusable">true</item>
        <item name="mirrorForRtl">true</item>
    </style>

我们找到了它的默认风格配置文件,可以看到它的基本属性都在这里了,来一条一条解释一下这些属性是什么意思:

indeterminateOnly 是否只是用于指示功能,很显然SeekBar除了指示还有拖拽,所以这里是false

progressDrawable 用于progress进度的背景色

indeterminateDrawable 用于progress指示进度的背景色

minHeight,maxHeight 两者相等,用于指定进度条的高度

thumb 用于指定滑动按钮的配置

thumbOffset 用于指定滑动按钮的偏移量,默认是8dp

好,主要的属性解释完,我们贴一下progress_horizontal文件中的内容,看看如何给progressBar配置背景色:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@id/background"
          android:gravity="center_vertical|fill_horizontal">
        <shape android:shape="rectangle"
               android:tint="?attr/colorControlNormal">
            <size android:height="@dimen/progress_bar_height_material" />
            <solid android:color="@color/white_disabled_material" />
        </shape>
    </item>
    <item android:id="@id/secondaryProgress"
          android:gravity="center_vertical|fill_horizontal">
        <scale android:scaleWidth="100%">
            <shape android:shape="rectangle"
                   android:tint="?attr/colorControlActivated">
                <size android:height="@dimen/progress_bar_height_material" />
                <solid android:color="@color/white_disabled_material" />
            </shape>
        </scale>
    </item>
    <item android:id="@id/progress"
          android:gravity="center_vertical|fill_horizontal">
        <scale android:scaleWidth="100%">
            <shape android:shape="rectangle"
                   android:tint="?attr/colorControlActivated">
                <size android:height="@dimen/progress_bar_height_material" />
                <solid android:color="@color/white" />
            </shape>
        </scale>
    </item>
</layer-list>

这是个标准的布局文件,我们可以看到在它里面定义了这个属性android:gravity="center_vertical|fill_horizontal",在网上我们经常可以看到各种SeekBar的样式里面是没有填写这个属性的,我们如果直接使用就会遇到thumb与progress的中心不在同一水平位置。

最后再贴一下标准的seek_thumb文件:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<!-- This is the thumb on the seek bar. -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"
          android:state_window_focused="true"
          android:drawable="@drawable/seek_thumb_pressed" />

    <item android:state_focused="true"
          android:state_window_focused="true"
          android:drawable="@drawable/seek_thumb_selected" />

    <item android:state_selected="true"
          android:state_window_focused="true"
          android:drawable="@drawable/seek_thumb_selected" />

    <item android:drawable="@drawable/seek_thumb_normal" />

</selector>
我们在写自定义属性的时候,只用拷贝这个文件并更改相关的属性就可以。
目录
相关文章
|
4月前
|
Java 开发工具 Android开发
Android与iOS开发环境搭建全解析####
本文深入探讨了Android与iOS两大移动操作系统的开发环境搭建流程,旨在为初学者及有一定基础的开发者提供详尽指南。我们将从开发工具的选择、环境配置到第一个简单应用的创建,一步步引导读者步入移动应用开发的殿堂。无论你是Android Studio的新手还是Xcode的探索者,本文都将为你扫清开发道路上的障碍,助你快速上手并享受跨平台移动开发的乐趣。 ####
|
3月前
|
存储 Linux API
深入探索Android系统架构:从内核到应用层的全面解析
本文旨在为读者提供一份详尽的Android系统架构分析,从底层的Linux内核到顶层的应用程序框架。我们将探讨Android系统的模块化设计、各层之间的交互机制以及它们如何共同协作以支持丰富多样的应用生态。通过本篇文章,开发者和爱好者可以更深入理解Android平台的工作原理,从而优化开发流程和提升应用性能。
|
3月前
|
Java 调度 Android开发
安卓与iOS开发中的线程管理差异解析
在移动应用开发的广阔天地中,安卓和iOS两大平台各自拥有独特的魅力。如同东西方文化的差异,它们在处理多线程任务时也展现出不同的哲学。本文将带你穿梭于这两个平台之间,比较它们在线程管理上的核心理念、实现方式及性能考量,助你成为跨平台的编程高手。
|
4月前
|
开发框架 Dart Android开发
安卓与iOS的跨平台开发:Flutter框架深度解析
在移动应用开发的海洋中,Flutter作为一艘灵活的帆船,正引领着开发者们驶向跨平台开发的新纪元。本文将揭开Flutter神秘的面纱,从其架构到核心特性,再到实际应用案例,我们将一同探索这个由谷歌打造的开源UI工具包如何让安卓与iOS应用开发变得更加高效而统一。你将看到,借助Flutter,打造精美、高性能的应用不再是难题,而是变成了一场创造性的旅程。
|
4月前
|
安全 Java Linux
深入解析Android系统架构及其对开发者的意义####
【10月更文挑战第21天】 本文旨在为读者揭开Android操作系统架构的神秘面纱,探讨其如何塑造现代移动应用开发格局。通过剖析Linux内核、硬件抽象层、运行时环境及应用程序框架等关键组件,揭示Android平台的强大功能与灵活性。文章强调了理解Android架构对于开发者优化应用性能、提升用户体验的重要性,并展望了未来技术趋势下Android的发展方向。 ####
95 0
|
4月前
|
安全 5G Android开发
安卓与iOS的较量:技术深度解析
【10月更文挑战第24天】 在移动操作系统领域,安卓和iOS无疑是两大巨头。本文将深入探讨这两个系统的技术特点、优势和不足,以及它们在未来可能的发展方向。我们将通过对比分析,帮助读者更好地理解这两个系统的本质和内涵,从而引发对移动操作系统未来发展的深思。
89 0
|
5月前
|
XML 存储 Java
浅谈Android的TextView控件
浅谈Android的TextView控件
62 0
|
4月前
|
监控 Java 应用服务中间件
高级java面试---spring.factories文件的解析源码API机制
【11月更文挑战第20天】Spring Boot是一个用于快速构建基于Spring框架的应用程序的开源框架。它通过自动配置、起步依赖和内嵌服务器等特性,极大地简化了Spring应用的开发和部署过程。本文将深入探讨Spring Boot的背景历史、业务场景、功能点以及底层原理,并通过Java代码手写模拟Spring Boot的启动过程,特别是spring.factories文件的解析源码API机制。
140 2
|
3天前
|
机器学习/深度学习 自然语言处理 算法
生成式 AI 大语言模型(LLMs)核心算法及源码解析:预训练篇
生成式 AI 大语言模型(LLMs)核心算法及源码解析:预训练篇
|
3月前
|
设计模式 存储 安全
【23种设计模式·全精解析 | 创建型模式篇】5种创建型模式的结构概述、实现、优缺点、扩展、使用场景、源码解析
创建型模式的主要关注点是“怎样创建对象?”,它的主要特点是"将对象的创建与使用分离”。这样可以降低系统的耦合度,使用者不需要关注对象的创建细节。创建型模式分为5种:单例模式、工厂方法模式抽象工厂式、原型模式、建造者模式。
【23种设计模式·全精解析 | 创建型模式篇】5种创建型模式的结构概述、实现、优缺点、扩展、使用场景、源码解析

推荐镜像

更多