您的位置:首页 > 移动开发 > Android开发

一个简单的自定义SwitchButton

2016-06-06 10:43 525 查看

一个简单的自定义Switchbutton

xml中定义:

<Switch
style="@style/widget_gender_switch"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:checked="false"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:showText="true"
android:textColor="@drawable/switch_text"
android:textOff="关"
android:textOn="开"
android:id="@+id/switch_resume"/>


style定义:

<style name="widget_gender_switch">
<item name="android:switchTextAppearance">@style/Sw</item>
<!-- 此设置不需要文字,只显示背景图的开关 -->
<item name="android:thumb">@drawable/widget_gender_thumb</item>
<!--滑块格式9.png -->
<item name="android:thumbTextPadding">20dp</item>
<item name="android:track">@drawable/widget_gender_track</item>
<!-- 背景图的开关 格式9.png-->
</style>
<style name="Sw" parent="@android:style/TextAppearance.Holo.Small">
<!-- Switch thumb asset presents a dark background. -->
<item name="android:textColor">#fff</item>
</style>


效果图:



style定义Sw 中 一定要继承@android:style/TextAppearance.Holo.Small

否则字的颜色显示可能存在一定问题,在某些设备上可能颜色会是黑色.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android switch