##1. android.support.v7.widget.SwitchCompatandroid
SwitchCompat
是早期switch的升级版,更美观还增长了滑动动画,兼容android5.0如下,使用这个控件须要在build.gradle
文件中增长官方提供的依赖包compile "com.android.support:appcompat-v7:25.1.1
bash
下面是基本的实现方法: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" /> ```

像上面这样的写法它系统默认获取`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" />
复制代码

哈哈,自定义颜色完成了,进行一下其余的修改吧,
`app:switchMinWidth="50dp"`自定义宽度
` app:thumbTintMode="src_in"`去掉开关阴影
添加监听事件:
复制代码
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

加上`android:foreground="?attr/selectableItemBackground"`效果棒棒哒
复制代码