[关闭]
@cxm-2016 2016-08-29T15:20:52.000000Z 字数 1229 阅读 1997

Switch Button

未分类


选择按钮

How to add?

I. In your build.gradle add latest appcompat library.

  1. dependencies {
  2. compile 'com.android.support:appcompat-v7:X.X.X' // where X.X.X version
  3. }

II. Make your activity extend android.support.v7.app.AppCompatActivity.

  1. public class MainActivity extends AppCompatActivity {
  2. ...
  3. }

III. Declare your SwitchCompat inside any layout.xml file

  1. <android.support.v7.widget.SwitchCompat
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:checked="true"/>

SwitchCompat is a backward compatible version of the Switch widget.

How to style?


I. Declare custom style in your styles.xml file.

  1. <style name="MySwitch" parent="Theme.AppCompat.Light">
  2. <!-- active thumb & track color (30% transparency) -->
  3. <item name="colorControlActivated">@color/indigo</item>
  4. <!-- inactive thumb color -->
  5. <item name="colorSwitchThumbNormal">@color/pink</item>
  6. <!-- inactive track color (30% transparency) -->
  7. <item name="android:colorForeground">@color/grey</item>
  8. </style>

II. Apply this style to your SwitchCompat via android:theme attribute.

  1. <android.support.v7.widget.SwitchCompat
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:checked="true"
  5. android:theme="@style/MySwitch"/>

Android automatically add 30% transparency to colorControlActivated and android:colorForeground for SwitchCompat.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注