1. android.support.v7.widget.SwitchCompat
SwitchCompat
是早期switch的升級版,更美觀還增加了滑動動畫,兼容android5.0以下,使用這個控件需要在build.gradle
文件中增加官方提供的依賴包compile "com.android.support:appcompat-v7:25.1.1
下面是基本的實現(xiàn)方法:
<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" />
像上面這樣的寫法它系統(tǒng)默認獲取color.xml
文件的 <color name="colorAccent">#FF4081</color>
中的顏色,如果我們需要改變顏色和樣式的話那就需要自定義屬性了艺谆,思想自定義SwitchCompat也很簡單锦溪!請繼續(xù)往下看
1.1 自定義SwitchCompat
假如我要把粉紅色改成藍色的話那就自定義style咯头朱,在style.xml文件中增加style.
android提供了屬性供我們自定義,是不是很貼心兽泣。
<style name="MySwitch" parent="Theme.AppCompat.Light">
<!--開啟時的顏色-->
<item name="colorControlActivated">@color/colorPrimary</item>
<!--關(guān)閉時的顏色-->
<item name="colorSwitchThumbNormal">@color/divider_color</item>
<!--關(guān)閉時的軌跡顏色取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" />
哈哈,自定義顏色完成了鲫尊,進行一下其他的修改吧,
app:switchMinWidth="50dp"
自定義寬度app:thumbTintMode="src_in"
去掉開關(guān)陰影
添加監(jiān)聽事件:
mSc.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
isChecked = true;
} else {
isChecked = false;
}
}
});
上面是使用SwitchCompat常用到的屬性沦偎,不常用到的屬性像什么給這個按鈕增加文字文字間距什么的我就不介紹了疫向,android提供的SwitchCompat我覺得挺好看的,只需要根據(jù)項目的主題自定義一下顏色就夠用了豪嚎,SwitchCompat就總結(jié)到這里啦搔驼,
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">
<TextView
android:text="CardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:textSize="25sp"
android:id="@+id/textView" />
</android.support.v7.widget.CardView>
CardView三個個主要的屬性就是app:cardElevation="10dp"陰影的大小
和 app:cardCornerRadius="10dp"卡片的圓角大小
app:card_view:contentPadding="5dp" 卡片內(nèi)容于邊距的間隔
通常都是4dp左右妄荔,陰影效果如果太大了看起來很丑的哦泼菌,這里為了演示所以設(shè)置10dp
加上
android:foreground="?attr/selectableItemBackground"
效果棒棒噠