Clo*_*all 47 android colors switchcompat
我尝试使用以下链接更改SwitchCompat的颜色:
注意我的开关中的低对比度:
但在更改所有相关颜色值后,SwitchCompat的轨道(亮灰色)保持不变.除了颜色,我不想改变外观.拇指是粉红色的,我希望轨道有一些对比.我错过了在styles.xml中定义一个值吗?
我尝试了这些值(随机非白色):
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/first</item>
<item name="colorPrimaryDark">@color/second</item>
<item name="colorAccent">@color/third</item>
...
<item name="colorControlActivated">@color/first</item>
<item name="colorControlHighlight">@color/first</item>
<item name="colorControlNormal">@color/second</item>
<item name="colorSwitchThumbNormal">@color/second</item>
<item name="colorButtonNormal">@color/second</item>
...>
Run Code Online (Sandbox Code Playgroud)
小智 146
我有同样的问题并解决了它.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<!-- Active thumb color & Active track color(30% transparency) -->
<item name="colorControlActivated">@color/theme</item>
<!-- Inactive thumb color -->
<item name="colorSwitchThumbNormal">@color/grey300</item>
<!-- Inactive track color(30% transparency) -->
<item name="android:colorForeground">@color/grey600</item>
...
</style>
Run Code Online (Sandbox Code Playgroud)
我阅读app compat代码,并了解它.
android.support.v7.internal.widget.TintManager.java
private ColorStateList getSwitchTrackColorStateList() {
if (mSwitchTrackStateList == null) {
final int[][] states = new int[3][];
final int[] colors = new int[3];
int i = 0;
// Disabled state
states[i] = new int[] { -android.R.attr.state_enabled };
colors[i] = getThemeAttrColor(android.R.attr.colorForeground, 0.1f);
i++;
states[i] = new int[] { android.R.attr.state_checked };
colors[i] = getThemeAttrColor(R.attr.colorControlActivated, 0.3f);
i++;
// Default enabled state
states[i] = new int[0];
colors[i] = getThemeAttrColor(android.R.attr.colorForeground, 0.3f);
i++;
mSwitchTrackStateList = new ColorStateList(states, colors);
}
return mSwitchTrackStateList;
}
Run Code Online (Sandbox Code Playgroud)
以下是针对特定SwitchCompat以编程方式更改轨道和拇指颜色的AppCompat方法.对于这个例子,我已经硬编码为红色.理想情况下,您可以通过第二个方法参数设置颜色.thumbColor
请注意,检查开关时会显示纹波.下面的代码不会改变纹波颜色.
public static void setSwitchColor(SwitchCompat v) {
// thumb color of your choice
int thumbColor = Color.RED;
// trackColor is the thumbColor with 30% transparency (77)
int trackColor = Color.argb(77, Color.red(thumbColor), Color.green(thumbColor), Color.blue(thumbColor));
// setting the thumb color
DrawableCompat.setTintList(v.getThumbDrawable(), new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_checked},
new int[]{}
},
new int[]{
thumbColor,
Color.WHITE
}));
// setting the track color
DrawableCompat.setTintList(v.getTrackDrawable(), new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_checked},
new int[]{}
},
new int[]{
trackColor,
Color.parseColor("#4D000000") // full black with 30% transparency (4D)
}));
}
Run Code Online (Sandbox Code Playgroud)
小智 7
如果您想对轨道的颜色进行总体化处理,可以使用此解决方案。
跟踪选择器.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/checked_color" android:state_checked="true" />
<item android:color="@color/checked_color" android:state_selected="true" />
<item android:color="@color/unchecked_color" android:state_checked="false" />
<item android:color="@color/unchecked_color" android:state_selected="false" />
Run Code Online (Sandbox Code Playgroud)
其中checked_color和unchecked_color是您选择的颜色。
styles.xml
<style name="mySwitchStyle" parent="@style/Widget.AppCompat.CompoundButton.Switch">
<!-- do here for additional costumization on thumb, track background,text appearance -->
</style>
<style name="mySwitchTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="switchStyle">@style/mySwitchStyle</item>
<item name="colorControlActivated">@color/red</item>
<item name="colorControlNormal">@color/colorAccent</item>
<item name="trackTint">@color/track_selector</item>
</style>
Run Code Online (Sandbox Code Playgroud)
布局文件
<android.support.v7.widget.SwitchCompat
android:theme="@style/mySwitchTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Run Code Online (Sandbox Code Playgroud)
使用MaterialSwitch
材料组件库提供的。
materialThemeOverlay
属性覆盖 Switch 的应用程序颜色。 <style name="CustomWidget.MaterialComponents.CompoundButton.Switch" parent="Widget.MaterialComponents.CompoundButton.Switch">
<item name="materialThemeOverlay">@style/CustomCompoundButton_Switch</item>
</style>
<style name="CustomCompoundButton_Switch" >
<item name="colorSurface">@color/yellow</item>
<item name="colorOnSurface">@color/orange</item>
<item name="colorControlActivated">@color/blue</item>
</style>
Run Code Online (Sandbox Code Playgroud)
并在布局中:
<com.google.android.material.switchmaterial.SwitchMaterial
style="@style/Widget.MaterialComponents.CompoundButton.Switch"
../>
Run Code Online (Sandbox Code Playgroud)
style
属性: <style name="CustomStyleWidget.MaterialComponents.CompoundButton.Switch"
parent="Widget.MaterialComponents.CompoundButton.Switch">
<item name="useMaterialThemeColors">false</item>
<item name="trackTint">@color/track_selector</item>
<item name="thumbTint">@color/thumb_selector</item>
</style>
Run Code Online (Sandbox Code Playgroud)
使用拇指选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="@color/switchTrackDisable"/>
<item android:state_checked="true" android:color="@color/switchThumbActive" />
<item android:color="@color/switchThumbkNormal" />
</selector>
Run Code Online (Sandbox Code Playgroud)
和轨道选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="@color/switchTrackDisable"/>
<item android:state_checked="true" android:color="@color/switchTrackActive" />
<item android:color="@color/switchTrackNormal" />
</selector>
Run Code Online (Sandbox Code Playgroud)
并在布局中:
<com.google.android.material.switchmaterial.SwitchMaterial
style="@style/CustomStyleWidget.MaterialComponents.CompoundButton.Switch"
../>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
32421 次 |
最近记录: |