【问题标题】:Android SwitchCompat not rendering track when in off stateAndroid SwitchCompat 在关闭状态时不呈现轨道
【发布时间】:2017-01-24 12:31:07
【问题描述】:

我的问题可能最好以视觉方式提出 -- 我想要一个 SwitchCompat 开关,使其看起来像他们在 Android 设置应用中的方式:

关闭:

这是在:

但由于某种原因,我的SwitchCompat 开关在关闭时看起来像这样:

没有灰色的“轨道”延伸到右侧。但是,当打开时,它看起来像预期的那样:

如您所见,我已将自定义色调应用于我的应用程序。我的自定义色调应用如下:

<activity
    android:name=".editor.MySettingsEditor"
    android:theme="@style/Theme.MyCustomTheme" />

然后,在styles.xml中:

<style name="Theme.MyCustomTheme" parent="Theme.AppCompat">    
    <item name="colorAccent">@color/myColorAccent</item>
    <item name="colorPrimary">@color/myColorPrimary</item>
    <item name="colorPrimaryDark">@color/myColorPrimaryDark</item>
    <item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item>
</style>

为确保不是我的自定义样式导致此问题,我通过以下操作将其删除:

<activity
    android:name=".editor.MySettingsEditor"
    android:theme="@style/Theme.AppCompat" />

但是,“关闭”轨道仍然没有显示,虽然色调现在变成了 Android 默认的蓝绿色。

为什么我的SwitchCompat开关在关闭状态时会丢失灰色轨道?

描述 SwitchCompat 的 XML 超级简单:

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

谢谢!

【问题讨论】:

  • 如果您禁用该自定义色调,您的曲目会返回吗?
  • 您究竟是如何“应用自定义色调”的?你不是只用你主题的colorControlActivated吗?
  • 我在我的问题中添加了更多详细信息,准确描述了我如何在此活动中进行颜色/主题自定义。 LMK 如果我可以或应该提供更多细节。谢谢!

标签: android switchcompat


【解决方案1】:

您可以通过将colorControlActivated 设置为活动颜色并将colorSwitchThumbNormal 设置为非活动颜色来应用自定义色调。如果你想改变轨道颜色那么你可以设置android:colorForeground

<style name="CustomSwitch" parent="Theme.AppCompat">  
    <!-- active thumb-->
    <item name="colorControlActivated">@color/active_switch_color</item>

    <!-- inactive thumb-->
    <item name="colorSwitchThumbNormal">@color/inactive_switch_color</item>

    <!-- inactive track color -->
    <item name="android:colorForeground">@color/inactive_track_color</item>
</style>  

如果您想直接为您的视图设置自定义主题:

<android.support.v7.widget.SwitchCompat
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:theme="@style/CustomSwitch"/>

【讨论】:

  • 就是这样!我应用了深色主题,因此我认为轨道颜色设置为白色,尽管在这个特定视图中我将开关渲染为白色背景,因此轨道颜色是白底白字,因此不可见。
  • 我错过了android:colorForeground。谢谢!
【解决方案2】:

@esilver 如果您想动态处理它们,也许是更好的方法

试试这个...我已经测试过了,它工作得很好

public class MainActivity extends AppCompatActivity {
    int[][] states = new int[][] {
            new int[] {-android.R.attr.state_checked},
            new int[] {android.R.attr.state_checked},
    };

    int[] thumbColors = new int[] {
            Color.BLACK,
            Color.RED,
    };

    int[] trackColors = new int[] {
            Color.GREEN,
            Color.BLUE,
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SwitchCompat switchCompat = (SwitchCompat) findViewById(R.id.switch);
        DrawableCompat.setTintList(DrawableCompat.wrap(switchCompat.getThumbDrawable()), new ColorStateList(states, thumbColors));
        DrawableCompat.setTintList(DrawableCompat.wrap(switchCompat.getTrackDrawable()), new ColorStateList(states, trackColors));
    }
}

您只需要根据您的要求/需要更新“trackColors”和“thumbColor”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    相关资源
    最近更新 更多