SwitchCompat和CardView使用總結

##1. android.support.v7.widget.SwitchCompatandroid

SwitchCompat是早期switch的升級版,更美觀還增長了滑動動畫,兼容android5.0如下,使用這個控件須要在 build.gradle文件中增長官方提供的依賴包compile "com.android.support:appcompat-v7:25.1.1bash

下面是基本的實現方法:app

<android.support.v7.widget.SwitchCompat
        android:id="@+id/sc_receive_phone_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true" /> ```

![效果圖](http://upload-images.jianshu.io/upload_images/3384890-9d98a0ef67d4df22.png?imageMogr2/auto-orient/strip%7CimageView2/1/w/800)
像上面這樣的寫法它系統默認獲取`color.xml`文件的  `<color name="colorAccent">#FF4081</color>`中的顏色,若是咱們須要改變顏色和樣式的話那就須要自定義屬性了,思想自定義SwitchCompat也很簡單!請繼續往下看
#######1.1 自定義SwitchCompat
假如我要把粉紅色改爲藍色的話那就自定義style咯,在style.xml文件中增長style.
android提供了屬性供咱們自定義,是否是很貼心。
複製代碼
<style name="MySwitch" parent="Theme.AppCompat.Light">
    <!--開啓時的顏色-->
    <item name="colorControlActivated">@color/colorPrimary</item>
    <!--關閉時的顏色-->
    <item name="colorSwitchThumbNormal">@color/divider_color</item>
    <!--關閉時的軌跡顏色取30%的顏色-->
    <item name="android:colorForeground">@color/divider_color</item>
</style>
複製代碼
在佈局文件中給須要自定義控件中增長`  app:theme="@style/MySwitch"`
自定義樣式,
完整代碼以下:

複製代碼
<android.support.v7.widget.SwitchCompat
            android:id="@+id/sc_receive_phone_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:theme="@style/MySwitch" />
複製代碼
![效果圖.png](http://upload-images.jianshu.io/upload_images/3384890-e18558c33fe614c2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
哈哈,自定義顏色完成了,進行一下其餘的修改吧,
`app:switchMinWidth="50dp"`自定義寬度![](http://upload-images.jianshu.io/upload_images/3384890-92274266551f916f.png?imageMogr2/auto-orient/strip%7CimageView2/0/w/100)

` app:thumbTintMode="src_in"`去掉開關陰影![](http://upload-images.jianshu.io/upload_images/3384890-be42745606068a08.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

添加監聽事件:
複製代碼
mSc.setOnCheckedChangeListener(
            new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                                             boolean isChecked) {
                    if (isChecked) {
                        isChecked = true;
                    } else {
                        isChecked = false;
                    }
                }
            });
複製代碼
>上面是使用SwitchCompat經常使用到的屬性,不經常使用到的屬性像什麼給這個按鈕增長文字文字間距什麼的我就不介紹了,android提供的SwitchCompat我以爲挺好看的,只須要根據項目的主題自定義一下顏色就夠用了,SwitchCompat就總結到這裏啦,


##2. android.support.v7.widget.CardView
>CardView是android5.0增長的卡片式特性,它是一個佈局容器ViewGroup!在item列表中我很喜歡用這個控件,item的左右下會有陰影后會給人一種很清新的風格,它也須要增長android提供的依賴包`compile "com.android.support:cardview-v7:25.1.0"` 若是你不想添加依賴包的話....能夠本身自定義ViewGroup

基本使用方法:
複製代碼

<android.support.v7.widget.CardView android:layout_width="200dp" app:cardElevation="10dp" app:cardCornerRadius="10dp" android:layout_gravity="center_horizontal" android:layout_height="200dp"> </android.support.v7.widget.CardView>ide

CardView三個個主要的屬性就是`    app:cardElevation="10dp"陰影的大小`和 `app:cardCornerRadius="10dp"卡片的圓角大小` `app:card_view:contentPadding="5dp" 卡片內容於邊距的間隔` 一般都是4dp左右,陰影效果若是太大了看起來很醜的哦,這裏爲了演示因此設置10dp
![效果圖](http://upload-images.jianshu.io/upload_images/3384890-3df2e30fce48ac65.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
加上`android:foreground="?attr/selectableItemBackground"`效果棒棒噠

![效果圖.png](http://upload-images.jianshu.io/upload_images/3384890-71750c50d35614f1.gif?imageMogr2/auto-orient/strip)複製代碼
相關文章
相關標籤/搜索