zoukankan      html  css  js  c++  java
  • 一手遮天 Android

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

    一手遮天 Android - view(选择类): Switch 样式

    示例如下:

    /view/selection/SwitchDemo2.java

    /**
     * Switch - 状态切换控件
     *     setThumbResource() - thumb 的各种状态下的样式
     *     setTrackResource() - track 的各种状态下的样式
     *     setSwitchTextAppearance() - 文字(在 textOff 和 textOn 设置的文字)的样式
     */
    
    package com.webabcd.androiddemo.view.selection;
    
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.Switch;
    
    import com.webabcd.androiddemo.R;
    
    public class SwitchDemo2 extends AppCompatActivity {
    
        private Switch _switch2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_view_selection_switchdemo2);
    
            _switch2 = (Switch)findViewById(R.id.switch2);
    
            sample();
        }
    
        private void sample() {
            _switch2.setThumbResource(R.drawable.selector_switch_thumb);
            _switch2.setTrackResource(R.drawable.selector_switch_track);
    
            // 参见 values/styles.xml 中的“MySwitchTextAppearance”
            _switch2.setSwitchTextAppearance(this, R.style.MySwitchTextAppearance);
        }
    }
    
    

    /layout/activity_view_selection_switchdemo2.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <!--
            Switch - 状态切换控件
                switchMinWidth - 控件的宽度
                showText - 是否需要显示 textOff 和 textOn 指定的文本
                textOn - 选中状态时,thumb 上显示的文本
                textOff - 未选中状态时,thumb 上显示的文本
                thumb - thumb 的各种状态下的样式(参见 drawable/shape_switch_thumb_pressed)
                track - track 的各种状态下的样式(参见 drawable/shape_switch_thumb_pressed)
                switchTextAppearance - 文字(在 textOff 和 textOn 设置的文字)的样式(参见 values/styles.xml 中的“MySwitchTextAppearance”)
        -->
    
        <Switch
            android:id="@+id/switch1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:thumb="@drawable/selector_switch_thumb"
            android:track="@drawable/selector_switch_track" />
    
        <!--
            在 java 中设置 Switch 的 thumb, track, switchTextAppearance
        -->
        <Switch
            android:id="@+id/switch2"
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:showText="true"
            android:textOff="关"
            android:textOn="开"
            android:switchMinWidth="200dp" />
    
    </LinearLayout>
    
    
    

    /drawable/selector_switch_thumb.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!--
            state_pressed="true" - 按下
            state_pressed="false" - 抬起
        -->
    
        <item
            android:state_pressed="true"
            android:drawable="@drawable/shape_switch_thumb_pressed" />
        <item
            android:state_pressed="false"
            android:drawable="@drawable/shape_switch_thumb_unpressed" />
    </selector>
    

    /drawable/selector_switch_track.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!--
            state_checked="true" - 选中
            state_checked="false" - 未选中
        -->
    
        <item
            android:state_checked="true"
            android:drawable="@drawable/shape_switch_track_checked" />
        <item
            android:state_checked="false"
            android:drawable="@drawable/shape_switch_track_unchecked" />
    </selector>
    

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

  • 相关阅读:
    MapServer Tutorial——MapServer7.2.1教程学习——第一节:MapServer的基本配置管理,静态地图应用以及MapFile文件
    MapServer Tutorial——MapServer7.2.1教程学习——教程背景
    MapServer Tutorial——MapServer7.2.1教程学习(大纲)
    MapServer Configuring with IIS
    GDAL源码编译(32位)
    开机自启动相关程序
    异常:Invalid character found in the request target. The valid characters are defined in RFC 3986
    如何将解压版的tomcat设置为windows 服务启动
    修改Tomcat控制台标题
    Tomcat控制台中文乱码解决办法
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_view_selection_SwitchDemo2.html
Copyright © 2011-2022 走看看