在Android Switch组件上定义自定义样式(主题)

Too*_*NeT 12 xml android nine-patch android-layout android-theme

我是Android开发的新手,也是Android主题/定制的新手,看起来是一个广泛的主题......

我正在尝试给我的Switch组件一个特殊的外观和感觉,但我无法实现我想要的东西,avan在互联网上查找了很多资源.

这真让我抓狂!!
在此先感谢您的帮助,Android大师!

上下文

我在一个现有的android应用程序(v1)上工作,它是minSdkVersion ="8".

因此,应用程序使用第三方库来获取操作栏(ActionBar Sherlock)和开关(SwitchCompatLibrary):

今天,我们正在制作v2版本,minSdkVersion ="14".

我们的客户还要求我们更改默认的开关外观.

目标是使用thiese开关:
在此输入图像描述 在此输入图像描述

这看起来真的像最新的材料设计开关,但用橙色代替"绿 - 蓝"颜色,就像这里一样.

约束

由于我们现在是minSdk 14,我们可以删除2个库"ActionBarSherlock"和"SwitchCompatLibrary",但这不是一个选项.的确,我们没有时间......

实际上,我尝试在我的项目的gradle文件appcompat-v7中添加依赖项,以便尝试使用带有材质主题的本机开关组件(请参阅此处),但这会因为重复的属性定义而提到错误,并提到了2个库以上.所以我不能用它,我不确定这应该有用......

dependencies {
(...)

compile project(':actionbarsherlock')
compile project(':switchCompatLibrary')
compile files('libs/android-support-v4.jar')

// incompatible avec actionbarsherlock and SwitchCompatLibrary...
// compile "com.android.support:appcompat-v7:22.0.+" 

(...)
}
Run Code Online (Sandbox Code Playgroud)

现有代码

所以这是执行代码.

在我的layout.xml文件中,我使用SwitchCompatLibrary开关:

        <de.ankri.views.Switch
            android:id="@+id/switchButtonNotification"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" />
Run Code Online (Sandbox Code Playgroud)

在我的themes.xml文件中:

<style name="Theme.Orange" parent="@style/Theme.Sherlock">
    ...
    <!-- Theme switch with SwitchCompatLibrary -->
    <item name="switchStyle">@style/switch_light</item>
    <item name="textAppearance">@style/TextAppearance</item>
</style>
Run Code Online (Sandbox Code Playgroud)

使用SwitchCompatLibrary本身定义的样式信息

styles.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="switch_light">
        <item name="track">@drawable/switch_track_holo_light</item>
        <item name="thumb">@drawable/switch_inner_holo_light</item>
        <item name="textOn">@string/textOn</item>
        <item name="textOff">@string/textOff</item>
        <item name="thumbTextPadding">12dip</item>
        <item name="switchMinWidth">96dip</item>
        <item name="switchPadding">16dip</item>
        <item name="switchTextAppearance">@style/TextAppearance</item>
    </style>    

    <style name="TextAppearance">
        <item name="textColor">?android:attr/textColorPrimary</item>
        <item name="textColorHighlight">?android:attr/textColorHighlight</item>
        <item name="textColorHint">?android:attr/textColorHint</item>
        <item name="textColorLink">?android:attr/textColorLink</item>
        <item name="textSize">16sp</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

switch_inner_holo_light.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/switch_thumb_disabled_holo_light" />
    <item android:state_pressed="true"  android:drawable="@drawable/switch_thumb_pressed_holo_light" />
    <item android:state_checked="true"  android:drawable="@drawable/switch_thumb_activated_holo_light" />
    <item                               android:drawable="@drawable/switch_thumb_holo_light" />
</selector>
Run Code Online (Sandbox Code Playgroud)

switch_track_holo_light.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true"  android:drawable="@drawable/switch_bg_focused_holo_light" />
    <item                               android:drawable="@drawable/switch_bg_holo_light" />
</selector>
Run Code Online (Sandbox Code Playgroud)

结果如下:
在此输入图像描述 在此输入图像描述

我尝试了什么

使用本机开关

由于我现在是最低API 14,我首先尝试用layout.xml中的"android.widget.Switch"替换"de.ankri.views.Switch",但样式不再适用(蓝色激活的开关代替或橙色)...
在此输入图像描述 在此输入图像描述

但是,直接在每个开关中定义主题(如此处所述)似乎更好,因为我有一个橙色切换回:

    <Switch
        android:id="@+id/switchButtonNotification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:checked="true"
        android:thumb="@drawable/switch_inner_holo_light"
        android:track="@drawable/switch_track_holo_light"
        android:layout_alignParentRight="true" />
Run Code Online (Sandbox Code Playgroud)

奇怪......我不知道为什么,所以我不会这样做,并保留"de.ankri.views.Switch"组件.

使用SwitchCompatLibrary开关并执行自己的样式

然后我尝试保留"de.ankri.views.Switch"组件并执行与SwitchCompatLibrary相同的操作,但使用我自己的样式覆盖"@ style/switch_light"样式,使用新的drawables for track and thumb

themes.xml:

<style name="Theme.Orange" parent="@style/Theme.Sherlock">
    ...
    <!-- Theme switch with SwitchCompatLibrary -->
    <item name="switchStyle">@style/Switch.Orange</item>
    <item name="textAppearance">@style/TextAppearance</item>
</style>
Run Code Online (Sandbox Code Playgroud)

styles.xml:

<style name="Switch.Orange" parent="@style/switch_light">
    <item name="track">@drawable/switch_track_orange</item>
    <item name="thumb">@drawable/switch_thumb_orange</item>
    <item name="textOn">@string/textOn</item>
    <item name="textOff">@string/textOff</item>
    <item name="thumbTextPadding">12dip</item>
    <item name="switchMinWidth">96dip</item>
    <item name="switchPadding">16dip</item>
    <item name="switchTextAppearance">@style/TextAppearance</item>
</style>
Run Code Online (Sandbox Code Playgroud)

switch_thumb_orange.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/switch_thumb_normal_orange" />
    <item android:state_pressed="true"  android:drawable="@drawable/switch_thumb_activated_orange" />
    <item android:state_checked="true"  android:drawable="@drawable/switch_thumb_activated_orange" />
    <item                               android:drawable="@drawable/switch_thumb_normal_orange" />
</selector>
Run Code Online (Sandbox Code Playgroud)

switch_thumb_activated_orange.9.png 在此输入图像描述

switch_thumb_normal_orange.9.png 在此输入图像描述

switch_track_orange.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/switch_track_normal_orange" />
    <item android:state_checked="true" android:drawable="@drawable/switch_track_activated_orange" />
    <item android:state_focused="true"  android:drawable="@drawable/switch_track_activated_orange" />
    <item                               android:drawable="@drawable/switch_track_normal_orange" />
</selector>
Run Code Online (Sandbox Code Playgroud)

switch_track_activated_orange.9.png 在此输入图像描述

switch_track_normal_orange.9.png 在此输入图像描述

结果:

结果就是AWFUL !! :
在此输入图像描述 在此输入图像描述

我哪里错了?我试图在styles.xml中更改"thumbTextPadding","switchMinWidth"和"switchPadding",但没有很好的结果.

也许我的9补丁文件错了?

Hib*_*bem 5

这篇文章描述了你需要什么:http : //www.materialdoc.com/switch/

如果着色不起作用(api < 21),请查看 此 stackoverflow 帖子。

希望这对你有帮助!


小智 -1

第一步,我建议您将 actionBarSherlock 替换为 support.v7.app.ActionBar。(https://developer.android.com/reference/android/support/v7/app/ActionBar.html

此迁移需要一些时间,因为您必须重新定义现有样式。在我的记忆中,当我进行类似的迁移时,v7样式的代码看起来像actionBarSherlock样式。此时,v7 ActionBar 被命名为 ActionBarCompat。