羊皮書APP(Android版)開發(fā)系列(二十七)一分鐘輕松修改CheckBox漓糙、RadioButton、RatingBar烘嘱、SeekBar樣式

我們在開發(fā)過程中經(jīng)常會用到CheckBox昆禽、RadioButton、RatingBar蝇庭、SeekBar 這幾個控件醉鳖,而系統(tǒng)默認的樣式卻不能滿足我們的要求,通常我們會直接采用github的開源庫來完成遗契,但是辐棒,這么一個簡單的小功能引入其他的開源庫實在不劃算,因此還是自己定義樣式比較好牍蜂。

先看下效果圖:

效果圖

要是實現(xiàn)上圖效果漾根,首先我們在colors.xml中定義四種顏色:

    <color name="firstColor">#00BCD4</color>
    <color name="secondColor">#E91E63</color>
    <color name="thirdColor">#FF5722</color>
    <color name="fourthColor">#4CAF50</color>

然后在styles.xml中定義四種樣式:

<style name="FirstControl" parent="Theme.AppCompat.Light">
        <item name="colorControlNormal">@color/firstColor</item>
        <item name="colorControlActivated">@color/firstColor</item>
    </style>

    <style name="SecondControl" parent="Theme.AppCompat.Light">
        <item name="colorControlNormal">@color/secondColor</item>
        <item name="colorControlActivated">@color/secondColor</item>
    </style>

    <style name="ThirdControl" parent="Theme.AppCompat.Light">
        <item name="colorControlNormal">@color/thirdColor</item>
        <item name="colorControlActivated">@color/thirdColor</item>
    </style>

    <style name="GreenControl" parent="Theme.AppCompat.Light">
        <item name="colorControlNormal">@color/fourthColor</item>
        <item name="colorControlActivated">@color/fourthColor</item>
    </style>

最后在布局文件中使用這四種樣式即可:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:theme="@style/FirstControl" />

        <RadioButton
            android:layout_weight="1"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:theme="@style/SecondControl" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:layout_weight="1"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:theme="@style/ThirdControl" />

        <RadioButton
            android:layout_weight="1"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:theme="@style/GreenControl" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox
            android:layout_weight="1"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:theme="@style/FirstControl" />

        <CheckBox
            android:layout_weight="1"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:theme="@style/SecondControl" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox
            android:layout_weight="1"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:theme="@style/ThirdControl" />

        <CheckBox
            android:layout_weight="1"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:theme="@style/GreenControl" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numStars="3"
            android:rating="1"
            android:theme="@style/FirstControl" />

        <RatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numStars="3"
            android:rating="1"
            android:theme="@style/SecondControl" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numStars="3"
            android:rating="2"
            android:theme="@style/ThirdControl" />

        <RatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numStars="3"
            android:rating="3"
            android:theme="@style/GreenControl" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <SeekBar
            android:layout_weight="1"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:theme="@style/FirstControl" />

        <SeekBar
            android:layout_weight="1"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:theme="@style/SecondControl" />

        <SeekBar
            android:layout_weight="1"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:theme="@style/ThirdControl" />

        <SeekBar
            android:layout_weight="1"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:theme="@style/GreenControl" />

    </LinearLayout>

</LinearLayout>

總結(jié):通過一個簡單的樣式定義,即可完成對android原生控件樣式的修改鲫竞,省時省力省資源辐怕。趕緊試試吧!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末从绘,一起剝皮案震驚了整個濱河市寄疏,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌僵井,老刑警劉巖陕截,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異批什,居然都是意外死亡农曲,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來乳规,“玉大人形葬,你說我怎么就攤上這事∧旱模” “怎么了笙以?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長冻辩。 經(jīng)常有香客問我猖腕,道長,這世上最難降的妖魔是什么微猖? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任谈息,我火速辦了婚禮,結(jié)果婚禮上凛剥,老公的妹妹穿的比我還像新娘。我一直安慰自己轻姿,他們只是感情好犁珠,可當我...
    茶點故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著互亮,像睡著了一般犁享。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上豹休,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天炊昆,我揣著相機與錄音,去河邊找鬼威根。 笑死凤巨,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的洛搀。 我是一名探鬼主播敢茁,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼留美!你這毒婦竟也來了彰檬?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤谎砾,失蹤者是張志新(化名)和其女友劉穎逢倍,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體景图,經(jīng)...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡较雕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了症歇。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片郎笆。...
    茶點故事閱讀 39,773評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡谭梗,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出宛蚓,到底是詐尸還是另有隱情激捏,我是刑警寧澤,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布凄吏,位于F島的核電站远舅,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏痕钢。R本人自食惡果不足惜图柏,卻給世界環(huán)境...
    茶點故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望任连。 院中可真熱鬧蚤吹,春花似錦、人聲如沸随抠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽拱她。三九已至二驰,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間秉沼,已是汗流浹背桶雀。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工唬复, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留漠魏,地道東北人柱锹。 一個月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像瞧毙,于是被迫代替她去往敵國和親宙彪。 傳聞我的和親對象是個殘疾皇子释漆,可洞房花燭夜當晚...
    茶點故事閱讀 44,689評論 2 354

推薦閱讀更多精彩內(nèi)容