【问题标题】:Inbox SwitchCompat button收件箱 SwitchCompat 按钮
【发布时间】:2015-09-13 18:28:34
【问题描述】:

今天我看到了收件箱应用程序。


(来源:cbsistatic.com)

我想知道如何在工具栏(searchButton 左侧)上制作那个开关按钮。 我没有找到参考(或者我没有搜索很多)。

谢谢^^

【问题讨论】:

    标签: android switchcompat google-inbox


    【解决方案1】:

    给你。 https://android-arsenal.com/details/1/1985

    您可以在同一站点中找到类似的材料。它很容易重用已经可用的东西。干杯!!编码愉快!

    【讨论】:

    • 非常感谢\o/
    【解决方案2】:

    这是一个老问题。但我想回答这个问题,因为我找到了一种无需使用任何第三方库即可实现相同目标的方法。

    1 - 将SwitchCompat 添加到您的布局中

     <android.support.v7.widget.SwitchCompat
                style="@style/SwitchCompatStyle"
                android:id="@+id/toolbar_switch"
               android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"/>
    

    2 - 创建一个 XML 形状。 switch_thumb_on.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="oval">
                <!--shadow color-->
                <solid android:color="@color/colorGray2" />
            </shape>
        </item>
        <!--padding to have shodow effect-->
        <item
            android:bottom="1dp"
            android:left="0.5dp"
            android:right="0.5dp"
            android:top="0.5dp">
    
            <!--switch thumb color-->
            <shape android:shape="oval">
                <solid android:color="@color/colorWhite" />
            </shape>
        </item>
        <item
            android:bottom="1dp"
            android:left="0.5dp"
            android:right="0.5dp"
            android:top="0.5dp">
    
            <!--switch image-->
            <!--NOTE: switch_color_selector is not a color but a selector-->
            <bitmap
                android:src="@drawable/ic_network_on"
                android:tint="@color/switch_color_selector" />
        </item>
    </layer-list>
    

    ic_network_on 是显示在开关拇指上的图标。如果您愿意,可以在此处添加状态/选择器。 switch_color_selector根据开关所在的状态给图标着色。

    3 - 这是颜色选择器switch_color_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/colorAccent" android:state_checked="true" />
        <item android:color="@color/colorGray1" />
    </selector>
    

    4 - 创建要应用于第一步添加的 Switch 的样式。

    <style name="SwitchCompatStyle" parent="Widget.AppCompat.CompoundButton.Switch" >
            <item name="android:thumb">@drawable/switch_thumb_on</item>
            <item name="trackTint">@color/switch_color_selector</item>
        </style>
    

    这就是我得到的结果。

    干杯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多