@cxm-2016
2016-08-29T15:20:52.000000Z
字数 1229
阅读 2006
未分类
选择按钮
I. In your build.gradle
add latest appcompat
library.
dependencies {
compile 'com.android.support:appcompat-v7:X.X.X' // where X.X.X version
}
II. Make your activity extend android.support.v7.app.AppCompatActivity
.
public class MainActivity extends AppCompatActivity {
...
}
III. Declare your SwitchCompat
inside any layout.xml
file
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"/>
SwitchCompat is a backward compatible version of the Switch widget.
I. Declare custom style in your styles.xml
file.
<style name="MySwitch" parent="Theme.AppCompat.Light">
<!-- active thumb & track color (30% transparency) -->
<item name="colorControlActivated">@color/indigo</item>
<!-- inactive thumb color -->
<item name="colorSwitchThumbNormal">@color/pink</item>
<!-- inactive track color (30% transparency) -->
<item name="android:colorForeground">@color/grey</item>
</style>
II. Apply this style to your SwitchCompat
via android:theme
attribute.
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:theme="@style/MySwitch"/>
Android automatically add 30% transparency to
colorControlActivated
andandroid:colorForeground
forSwitchCompat
.