新思想

ToggleButton 和 Switch

 

ToggleButtonSwitch

 

styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="MyToggleButton" parent="@android:style/Widget.CompoundButton">
        <item name="android:button">@drawable/settingbutton</item>
    </style>
</resources>

settingbutton.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/on_button" android:state_checked="true" />
    <item android:drawable="@drawable/off_button" android:state_checked="false" />
</selector>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.xiesir.example09togglebuttonswitch.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="一、基本属性\n
textOff:没选中时文字,textOn:选中时文字\n
checked / setChecked():是否被选中\n
1) ToggleButton\n
disabledAlpha:设置禁用时的透明度" />

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:textColor="@color/colorAccent"
        android:textOn="放开我"
        android:textOff="缠住你" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2) Switch\n
showText:是否显示文字\n
track / thumb:底部 / 滑块的图片\n
splitTrack:是否设置滑块与底部图片间的间隙\n
switchPadding:设置文字的间隔" />

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:showText="true"
        android:textOn="美女"
        android:textOff="帅哥" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="二、举例\n
ToggleButton、Switch、CheckBox和RadioButton都是继承自android.widget.CompoundButton,用法类似" />

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/MyToggleButton"
        android:checked="true"
        android:textOff="Toggle关闭"
        android:textOn="Toggle打开" />

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:layout_weight="0.5"
        android:textOff="Switch关闭"
        android:textOn="Switch打开"
        android:thumb="@drawable/tb_thumb"
        android:track="@drawable/tb_track" />
    <!--android:gravity="center"-->

    <CheckBox
        style="@style/MyToggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:textOff="CheckBox关闭"
        android:textOn="CheckBox打开" />

    <RadioButton
        style="@style/MyToggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:textOff="RadioButton关闭"
        android:textOn="RadioButton打开" />

</LinearLayout>

源程序下载

参考:

http://blog.csdn.net/coder_pig/article/details/47035699

posted on 2016-05-17 01:11  新思想  阅读(515)  评论(0)    收藏  举报

导航