Android控件默认风格解析之SeekBar
在我們開發(fā)的時(shí)候常常需要更改原生控件的默認(rèn)效果,有時(shí)候某些控件改起來(lái)挺費(fèi)勁的,比如SeekBar的背景與其ProgressBar的進(jìn)度粗細(xì)或者thumb居中現(xiàn)實(shí)與否如果弄錯(cuò),都是個(gè)大麻煩,我曾經(jīng)就為thumb的居中顯示問題浪費(fèi)了很多很多的時(shí)間,后來(lái)以別的笨拙的辦法解決了,現(xiàn)在重新回來(lái)看,決定下決心整一下,看看到底是怎么回事。
我們知道,當(dāng)我們?cè)趯懸粋€(gè)xml布局的時(shí)候,只需要簡(jiǎn)單的為這個(gè)控件指定一個(gè)高寬便可以看到它的效果,就像這樣:
<?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>預(yù)覽效果圖:
我們可以看到這個(gè)控件本身是帶有一種風(fēng)格的,那這個(gè)風(fēng)格在哪里被定義了呢?我們一起來(lái)找找:
我們當(dāng)然需要先去SeeBar的類里面找找有什么關(guān)鍵的信息:
我們?cè)赟eeBar的重載構(gòu)造方法中看到一條有用的信息:
public SeekBar(Context context, AttributeSet attrs) {this(context, attrs, com.android.internal.R.attr.seekBarStyle);}原來(lái)奧秘就在com.android.internal.R.attr.seekBarStyle的里面,我們找找去,seekBarStyle位于platform_frameworks_base-master\core\res\res\values\themes.xml文件中,在該文件中可以發(fā)現(xiàn)它的定義: <item name="seekBarStyle">@style/Widget.SeekBar</item>
看來(lái)它是調(diào)用了位于style下的Widget.SeekBar風(fēng)格,我們?cè)偃フ艺?#xff1a; <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>
我們找到了它的默認(rèn)風(fēng)格配置文件,可以看到它的基本屬性都在這里了,來(lái)一條一條解釋一下這些屬性是什么意思:
indeterminateOnly 是否只是用于指示功能,很顯然SeekBar除了指示還有拖拽,所以這里是false
progressDrawable 用于progress進(jìn)度的背景色
indeterminateDrawable?用于progress指示進(jìn)度的背景色
minHeight,maxHeight 兩者相等,用于指定進(jìn)度條的高度
thumb 用于指定滑動(dòng)按鈕的配置
thumbOffset 用于指定滑動(dòng)按鈕的偏移量,默認(rèn)是8dp
好,主要的屬性解釋完,我們貼一下progress_horizontal文件中的內(nèi)容,看看如何給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>這是個(gè)標(biāo)準(zhǔn)的布局文件,我們可以看到在它里面定義了這個(gè)屬性android:gravity="center_vertical|fill_horizontal",在網(wǎng)上我們經(jīng)??梢钥吹礁鞣NSeekBar的樣式里面是沒有填寫這個(gè)屬性的,我們?nèi)绻苯邮褂镁蜁?huì)遇到thumb與progress的中心不在同一水平位置。
最后再貼一下標(biāo)準(zhǔn)的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> 我們?cè)趯懽远x屬性的時(shí)候,只用拷貝這個(gè)文件并更改相關(guān)的屬性就可以。總結(jié)
以上是生活随笔為你收集整理的Android控件默认风格解析之SeekBar的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 决策树(西瓜书学习)
- 下一篇: Android官方开发文档Trainin