欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開(kāi)發(fā)實(shí)現(xiàn)Switch控件修改樣式功能示例【附源碼下載】

 更新時(shí)間:2019年04月04日 14:31:32   作者:極光舞者  
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)Switch控件修改樣式功能,涉及Android Switch開(kāi)關(guān)控件樣式設(shè)置與事件響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android開(kāi)發(fā)實(shí)現(xiàn)Switch控件修改樣式功能。分享給大家供大家參考,具體如下:

Android中自帶的Switch控件在很多時(shí)候總覺(jué)得和整體系統(tǒng)風(fēng)格不符,很多時(shí)候,自定義Switch是一種方法。

但其實(shí)不用這么麻煩,安卓自帶的Switch通過(guò)修改一些屬性,也可以達(dá)到和自定義Switch差不多的一個(gè)效果。

個(gè)人感覺(jué),Switch的屬性設(shè)置和其他控件還是有挺大區(qū)別的。因此,寫(xiě)下此文,方便有需要的同學(xué)參考。

先上效果圖:

以上便是修改后效果 與 原生Switch的效果對(duì)比。代碼在文章底部給出

實(shí)現(xiàn)方式:

1.底部滑動(dòng)條,在開(kāi)關(guān)打開(kāi)狀態(tài)為綠色,開(kāi)關(guān)關(guān)閉狀態(tài)為灰色

在 res/drawable 文件夾下面,寫(xiě)兩個(gè)滑動(dòng)條的底圖 ,通過(guò)一個(gè)選擇器selector進(jìn)行控制。

gray_track.xml :非打開(kāi)狀態(tài),灰色的底圖

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >
  <!-- 高度30  此處設(shè)置寬度無(wú)效-->
  <size android:height="30dp"/>
  <!-- 圓角弧度 15 -->
 <corners android:radius="15dp"/>
 <!-- 變化率 定義從左到右的顏色不變 -->
  <gradient
    android:endColor="#888888"
    android:startColor="#888888" />
</shape>

green_track.xml:打開(kāi)狀態(tài)下,綠色的底圖。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <!-- 高度40 -->
  <size android:height="30dp"/>
  <!-- 圓角弧度 20 -->
 <corners android:radius="15dp"/>
 <!-- 變化率 -->
  <gradient
    android:endColor="#33da33"
    android:startColor="#33da33" />
</shape>

選擇器 track.xml   用于控制Switch不同狀態(tài)下,滑動(dòng)條的底圖

<?xml version="1.0" encoding="utf-8"?>
<!-- 底層下滑條的樣式選擇器,可控制Switch在不同狀態(tài)下,底下下滑條的顏色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@drawable/green_track" />
<item                android:drawable="@drawable/gray_track" />
</selector>

2. 滑動(dòng)按鈕:底色我用的接近白色的淡灰色,打開(kāi)時(shí),邊上的一圈線條為灰色,關(guān)閉時(shí),邊上的一圈線條為綠色

實(shí)現(xiàn)方式和底部滑動(dòng)一致

gray_thumb.xml  :關(guān)閉狀態(tài),按鈕邊上一圈顏色為深灰色

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >
  <!-- 高度40 -->
  <size android:height="40dp" android:width="40dp"/>
  <!-- 圓角弧度 20 -->
 <corners android:radius="20dp"/>
 <!-- 變化率 -->
  <gradient
    android:endColor="#eeeeee"
    android:startColor="#eeeeee" />
  <stroke android:width="1dp"
    android:color="#666666"/>
</shape>

green_thumb.xml : 打開(kāi)狀態(tài),按鈕邊上一圈的顏色為綠色

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >
  <!-- 高度40 -->
  <size android:height="40dp" android:width="40dp"/>
  <!-- 圓角弧度 20 -->
 <corners android:radius="20dp"/>
 <!-- 變化率 -->
  <gradient
    android:endColor="#eeeeee"
    android:startColor="#eeeeee" />
  <stroke android:width="1dp"
    android:color="#33da33"/>
</shape>

選擇器 thumb.xml   用于控制Switch不同狀態(tài)下,按鈕的顯示狀態(tài)

<?xml version="1.0" encoding="utf-8"?>
<!-- 按鈕的選擇器,可以設(shè)置按鈕在不同狀態(tài)下的時(shí)候,按鈕不同的顏色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@drawable/green_thumb" />
<item                android:drawable="@drawable/gray_thumb" />
</selector>

3. 將以上選擇器設(shè)置給Switch,就好了

界面  activity_main.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"
  android:orientation="vertical" >
  <Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:switchMinWidth="20dp"
    android:textOn=" "
    android:textOff=" "
    android:thumb="@drawable/thumb"
    android:track="@drawable/track" />
  <!-- 用于對(duì)比使用的不設(shè)置任何屬性的Switch -->
  <Switch 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
</LinearLayout>

4.高度,寬度的設(shè)置

細(xì)心的同學(xué)會(huì)發(fā)現(xiàn),修改  android:layout_width  , android:layout_height  這兩個(gè)屬性,并不會(huì)實(shí)際修改Switch的大小

設(shè)置大了,邊上會(huì)出現(xiàn)空白部分,設(shè)置小了,Switch顯示不全。

實(shí)際設(shè)置高度方法:

上面定義滑動(dòng)條和按鈕底圖的地方相信大家都注意到,

<size android:height="30dp"/>

這行代碼,

修改  green_track.xml,gray_track.xml  中的高度,即可修改高度(修改green_thumb.xml  gray_thumb.xml  中的高度貌似無(wú)效)。

實(shí)際修改寬度的方法:

(1)修改滑動(dòng)按鈕的寬度:滑動(dòng)按鈕的寬度和按鈕上的文字有關(guān),

想要按鈕變長(zhǎng),在按鈕顯示的文字上添加幾個(gè)空字符串即可,想要按鈕變短的話,減少按鈕上顯示的字即可(修改按鈕上字體大小也可以試試)

Switch的屬性

android:textOn=" "
android:textOff=" "

(2)修改按鈕  打開(kāi),關(guān)閉  兩種狀態(tài)之間滑動(dòng)距離(貌似小到一定程度,再改小就無(wú)效了)

Switch的屬性

android:switchMinWidth="20dp"

通過(guò)以上的設(shè)置,相信能滿足大部分實(shí)際使用的需求了,希望對(duì)大家有幫助。

相信源代碼才是大家最關(guān)注的吧,哈哈,我也是!

下載地址:

點(diǎn)擊此處本站下載

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》及《Android資源操作技巧匯總

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論