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

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns: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"><SeekBarandroid: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 ProjectLicensed 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 athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations 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>

我们在写自定义属性的时候,只用拷贝这个文件并更改相关的属性就可以。

Android控件默认风格解析之SeekBar相关推荐

  1. Android控件默认点击效果

    Android控件默认点击效果 Android最简单的默认点击效果: android:background="?android:attr/selectableItemBackground&q ...

  2. html中radio单选按钮控件标签用法解析及如何设置默认选中

    Radio 对象代表 HTML 表单中的单选按钮.在 HTML 表单中 <input type="radio"> 每出现一次,一个 Radio 对象就会被创建.单选按钮 ...

  3. android控件属性

    android控件属性 控件属性: android属性 Android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面这篇文章结合了网上不少资料, 第一类:属性值为true或false an ...

  4. Android 控件 RecyclerView 看这篇就够了

    [Android 控件 RecyclerView] 概述 RecyclerView是什么 从Android 5.0开始,谷歌公司推出了一个用于大量数据展示的新控件RecylerView,可以用来代替传 ...

  5. Android 控件 RecyclerView

    [Android 控件 RecyclerView] 概述 RecyclerView是什么 从Android 5.0开始,谷歌公司推出了一个用于大量数据展示的新控件RecylerView,可以用来代替传 ...

  6. 【Android控件属性记录】

    #Android 控件属性记录 方便查找 控件属性: android属性 android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面这篇文章结合了网上不少资料, 第一类:属性值为true ...

  7. android 控件 堆叠_每次在Android上正确地向后堆叠

    android 控件 堆叠 When navigating in a mobile app, the screens opened after one another form a stack, th ...

  8. App控件定位:Android 控件介绍及元素定位方法

    本文将分享Android相关基础知识和Android APP控件定位工具的使用方法. 目录 Android基础知识 Android布局 Android四大组件 1.activity 2.Service ...

  9. Android控件点击圆形阴影反馈解决方案

    Android控件点击圆形阴影反馈解决方案 愿景 对于手机App而言,没有反馈效果的按钮使用体验是不舒服的.最近在深入学习Material Design,对其中的ripple效果比较感兴趣.我想着尝试 ...

最新文章

  1. Windows核心编程 第四章 进程(中)
  2. 如何读论文?-如何写好科技论文之我见(八)
  3. 标准库函数、系统调用的区别
  4. mysql数据去重_mysql 数据去重
  5. 被讨厌的勇气--总结
  6. 免费电子书:Azure Web Apps开发者入门
  7. Java设计模式-设计模式概述
  8. 大数据分析应用在哪些方面
  9. ArcGIS API for JavaScript与 npm
  10. Java小白入门200例54之打印水仙花数
  11. 程序员入门:三本必看的书
  12. vue2 配置本地IP地址访问项目
  13. misc类设备驱动1——板载蜂鸣器驱动测试
  14. sFTP 上传或下载文件与文件夹
  15. 【土旦】在vue filters中 优雅的使用对象的key、value来替换 if switch多重判断简化流程...
  16. 【计算机组成原理】程序,指令,机器指令,微程序,微指令,微命令,微操作的关系
  17. 云计算的特点包括哪几方面?
  18. 利用网线在两台电脑之间传输文件
  19. lzo的正确c语言代码,LZO 使用跟介绍
  20. vue触发模拟点击效果功能

热门文章

  1. 怎么让sublime text3可以运行c/c++
  2. 自己动手实现一个malloc内存分配器 | 30图
  3. ARM 原子操作里的两个汇编指令
  4. git上传大于100M的文件
  5. java cellvalue_Java Cell.getErrorCellValue方法代码示例
  6. 数据结构链表之栈,Python3简单实现——5
  7. 十七、PHP框架Laravel学习笔记——模型的定义
  8. Chapter1-2_Speech_Recognition(LAS)
  9. 牛客 牛牛的独特子序列(双指针/二分查找)
  10. LeetCode 157. 用 Read4 读取 N 个字符