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

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

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

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

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

但其實不用這么麻煩,安卓自帶的Switch通過修改一些屬性,也可以達到和自定義Switch差不多的一個效果。

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

先上效果圖:

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

實現(xiàn)方式:

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

在 res/drawable 文件夾下面,寫兩個滑動條的底圖 ,通過一個選擇器selector進行控制。

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

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

green_track.xml:打開狀態(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)下,滑動條的底圖

<?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. 滑動按鈕:底色我用的接近白色的淡灰色,打開時,邊上的一圈線條為灰色,關(guān)閉時,邊上的一圈線條為綠色

實現(xiàn)方式和底部滑動一致

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 : 打開狀態(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)下的時候,按鈕不同的顏色 -->
<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" />
  <!-- 用于對比使用的不設(shè)置任何屬性的Switch -->
  <Switch 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
</LinearLayout>

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

細心的同學會發(fā)現(xiàn),修改  android:layout_width  , android:layout_height  這兩個屬性,并不會實際修改Switch的大小

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

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

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

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

這行代碼,

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

實際修改寬度的方法:

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

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

Switch的屬性

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

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

Switch的屬性

android:switchMinWidth="20dp"

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

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

下載地址:

點擊此處本站下載。

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

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

相關(guān)文章

最新評論