Jam*_*rue 132 android togglebutton
我在ICS应用程序中使用带有holo.light主题的标准Switch控件.
我想将切换按钮的突出显示或状态颜色从标准浅蓝色更改为绿色.
这应该很容易,但我似乎无法弄清楚如何做到这一点.
Hit*_*ahu 236
迟到派对,但这就是我做的
样式
<style name="SCBSwitch" parent="Theme.AppCompat.Light">
<!-- active thumb & track color (30% transparency) -->
<item name="colorControlActivated">#46bdbf</item>
<!-- inactive thumb color -->
<item name="colorSwitchThumbNormal">#f1f1f1
</item>
<!-- inactive track color (30% transparency) -->
<item name="android:colorForeground">#42221f1f
</item>
</style>
Run Code Online (Sandbox Code Playgroud)
颜色
布局
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:checked="false"
android:theme="@style/SCBSwitch" />
Run Code Online (Sandbox Code Playgroud)
结果
请参阅启用和禁用开关的颜色更改
小智 94
截至目前,最好使用AppCompat.v7库中的SwitchCompat.然后,您可以使用简单的样式来更改组件的颜色.
values/themes.xml:
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/my_awesome_color</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">@color/my_awesome_darker_color</item>
<!-- colorAccent is used as the default value for colorControlActivated,
which is used to tint widgets -->
<item name="colorAccent">@color/accent</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight, and colorSwitchThumbNormal. -->
</style>
Run Code Online (Sandbox Code Playgroud)
参考:Android开发者博客
编辑:
它应该正确应用的方式是通过android:theme="@style/Theme.MyTheme"
,也可以应用于父样式,如EditTexts,RadioButtons,开关,CheckBoxes和ProgressBars:
<style name="My.Widget.ProgressBar" parent="Widget.AppCompat.ProgressBar">
<style name="My.Widget.Checkbox" parent="Widget.AppCompat.CompoundButton.CheckBox">
Run Code Online (Sandbox Code Playgroud)
arl*_*dia 48
这对我有用(需要Android 4.1):
Switch switchInput = new Switch(this);
int colorOn = 0xFF323E46;
int colorOff = 0xFF666666;
int colorDisabled = 0xFF333333;
StateListDrawable thumbStates = new StateListDrawable();
thumbStates.addState(new int[]{android.R.attr.state_checked}, new ColorDrawable(colorOn));
thumbStates.addState(new int[]{-android.R.attr.state_enabled}, new ColorDrawable(colorDisabled));
thumbStates.addState(new int[]{}, new ColorDrawable(colorOff)); // this one has to come last
switchInput.setThumbDrawable(thumbStates);
Run Code Online (Sandbox Code Playgroud)
请注意,需要最后添加"默认"状态,如此处所示.
我看到的唯一问题是开关的"拇指"现在看起来比开关的背景或"轨道"大.我想那是因为我还在使用默认的轨道图像,周围有一些空的空间.但是,当我尝试使用这种技术自定义轨道图像时,我的开关看起来高度为1像素,只有一小段开/关文本出现.必须有一个解决方案,但我还没有找到它...
Android 5的更新
在Android 5中,上面的代码使开关完全消失.我们应该能够使用新的setButtonTintList方法,但这似乎被忽略了.但这有效:
ColorStateList buttonStates = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_enabled},
new int[]{android.R.attr.state_checked},
new int[]{}
},
new int[]{
Color.BLUE,
Color.RED,
Color.GREEN
}
);
switchInput.getThumbDrawable().setTintList(buttonStates);
switchInput.getTrackDrawable().setTintList(buttonStates);
Run Code Online (Sandbox Code Playgroud)
Android 6-7更新
正如Cheruby在评论中所说的那样,我们可以使用新的setThumbTintList,并且按照我的预期运作.我们也可以使用setTrackTintList,但是它将颜色应用为混合颜色,结果在深色主题中比预期更暗,在浅色主题中比预期更亮,有时甚至是不可见的.在Android 7中,我能够通过覆盖轨道色调模式来最小化这种变化,但是我无法从Android 6中得到不错的结果.您可能需要定义额外的颜色来补偿混合.(您是否觉得Google不希望我们自定义应用程序的外观?)
ColorStateList thumbStates = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_enabled},
new int[]{android.R.attr.state_checked},
new int[]{}
},
new int[]{
Color.BLUE,
Color.RED,
Color.GREEN
}
);
switchInput.setThumbTintList(thumbStates);
if (Build.VERSION.SDK_INT >= 24) {
ColorStateList trackStates = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_enabled},
new int[]{}
},
new int[]{
Color.GRAY,
Color.LTGRAY
}
);
switchInput.setTrackTintList(trackStates);
switchInput.setTrackTintMode(PorterDuff.Mode.OVERLAY);
}
Run Code Online (Sandbox Code Playgroud)
Kev*_*OUX 35
要在不使用style.xml或Java代码的情况下更改切换样式,可以自定义切换到布局XML:
<Switch
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:thumbTint="@color/blue"
android:trackTint="@color/white"
android:checked="true"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
它的属性是android:thumbTint和android:trackTint,它允许你自定义颜色
这是此XML的可视结果:
rem*_*ife 33
作为现有答案的补充:您可以使用res/color文件夹中的选择器自定义拇指和轨道,例如:
switch_track_selector
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/lightBlue"
android:state_checked="true" />
<item android:color="@color/grey"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
switch_thumb_selector
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/darkBlue"
android:state_checked="true" />
<item android:color="@color/white"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
使用这些选择器自定义轨道和拇指色调:
<androidx.appcompat.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:trackTint="@color/switch_track_selector"
app:thumbTint="@color/switch_thumb_selector"/>
Run Code Online (Sandbox Code Playgroud)
请记住,如果你使用非标准Switch和android命名空间的这些属性,它只会为API 23及更高版本,所以在使用SwitchCompat与app命名空间xmlns:app="http://schemas.android.com/apk/res-auto"作为通用的解决方案。
结果:
fra*_*ssb 31
创建自定义切换并覆盖setChecked以更改颜色:
public class SwitchPlus extends Switch {
public SwitchPlus(Context context) {
super(context);
}
public SwitchPlus(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SwitchPlus(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setChecked(boolean checked) {
super.setChecked(checked);
changeColor(checked);
}
private void changeColor(boolean isChecked) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
int thumbColor;
int trackColor;
if(isChecked) {
thumbColor = Color.argb(255, 253, 153, 0);
trackColor = thumbColor;
} else {
thumbColor = Color.argb(255, 236, 236, 236);
trackColor = Color.argb(255, 0, 0, 0);
}
try {
getThumbDrawable().setColorFilter(thumbColor, PorterDuff.Mode.MULTIPLY);
getTrackDrawable().setColorFilter(trackColor, PorterDuff.Mode.MULTIPLY);
}
catch (NullPointerException e) {
e.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 25
制作可绘制的“newthumb.xml”
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/Green" android:state_checked="true"/>
<item android:color="@color/Red" android:state_checked="false"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
并制作可绘制的“newtrack.xml”
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/black" android:state_checked="true"/>
<item android:color="@color/white" android:state_checked="false"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
并将其添加到 Switch 中:
<Switch
android:trackTint="@drawable/newtrack"
android:thumbTint="@drawable/newthumb"
/>
Run Code Online (Sandbox Code Playgroud)
此外,完全可以只制作一个可绘制文件(“switchcolors.xml”)并将其用于 trackTint 和 thumbTint。
Gre*_*nis 20
虽然SubChord的回答是正确的,但是并没有真正回答如何在不影响其他小部件的情况下设置"开启"颜色的问题.为此,请ThemeOverlay在styles.xml中使用:
<style name="ToggleSwitchTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="colorAccent">@color/green_bright</item>
</style>
Run Code Online (Sandbox Code Playgroud)
并在您的交换机中引用它:
<android.support.v7.widget.SwitchCompat
android:theme="@style/ToggleSwitchTheme" ... />
Run Code Online (Sandbox Code Playgroud)
这样做只会影响要应用它的视图的颜色.
MCR*_*MCR 18
当Switch状态发生变化时,我通过更新滤色器来解决它...
public void bind(DetailItem item) {
switchColor(item.toggle);
listSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
switchColor(b);
}
});
}
private void switchColor(boolean checked) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
listSwitch.getThumbDrawable().setColorFilter(checked ? Color.BLACK : Color.WHITE, PorterDuff.Mode.MULTIPLY);
listSwitch.getTrackDrawable().setColorFilter(!checked ? Color.BLACK : Color.WHITE, PorterDuff.Mode.MULTIPLY);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 13
可能是它有点晚了,但对于切换按钮,toogle按钮不是答案,你必须更改交换机的xml参数中的drawable:
android:thumb="your drawable here"
Run Code Online (Sandbox Code Playgroud)
win*_*ear 10
<androidx.appcompat.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:thumbTint="@color/white"
app:trackTint="@drawable/checker_track"/>
Run Code Online (Sandbox Code Playgroud)
在checker_track.xml内部:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/lightish_blue" android:state_checked="true"/>
<item android:color="@color/hint" android:state_checked="false"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
小智 10
这对我有用 -:
1.values/styles.xml中的代码-:
<style name="SwitchTheme" parent="Theme.AppCompat.Light">
<item name="android:colorControlActivated">#148E13</item>
</style>
Run Code Online (Sandbox Code Playgroud)
2.在布局文件的开关中添加以下代码行:
android:theme="@style/SwitchTheme"
Run Code Online (Sandbox Code Playgroud)
创建自己的9补丁图像并将其设置为切换按钮的背景.
http://radleymarx.com/2011/simple-guide-to-9-patch/
在Android Lollipop及更高版本中,在您的主题样式中定义它:
<style name="BaseAppTheme" parent="Material.Theme">
...
<item name="android:colorControlActivated">@color/color_switch</item>
</style>
Run Code Online (Sandbox Code Playgroud)
arlomedia提出的解决方案为我工作.关于他的extraspace问题我解决了删除交换机的所有填充.
编辑
根据要求,我在这里.
在布局文件中,我的开关位于线性布局内并位于TextView之后.
<LinearLayout
android:id="@+id/myLinearLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal|center"
android:gravity="right"
android:padding="10dp"
android:layout_marginTop="0dp"
android:background="@drawable/bkg_myLinearLayout"
android:layout_marginBottom="0dp">
<TextView
android:id="@+id/myTextForTheSwitch"
android:layout_height="wrap_content"
android:text="@string/TextForTheSwitch"
android:textSize="18sp"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal|center"
android:gravity="right"
android:layout_width="wrap_content"
android:paddingRight="20dp"
android:textColor="@color/text_white" />
<Switch
android:id="@+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="@string/On"
android:textOff="@string/Off"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_toRightOf="@id/myTextForTheSwitch"
android:layout_alignBaseline="@id/myTextForTheSwitch"
android:gravity="right" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
由于我正在使用Xamarin/Monodroid(最低安卓4.1),我的代码是:
Android.Graphics.Color colorOn = Android.Graphics.Color.Green;
Android.Graphics.Color colorOff = Android.Graphics.Color.Gray;
Android.Graphics.Color colorDisabled = Android.Graphics.Color.Green;
StateListDrawable drawable = new StateListDrawable();
drawable.AddState(new int[] { Android.Resource.Attribute.StateChecked }, new ColorDrawable(colorOn));
drawable.AddState(new int[] { -Android.Resource.Attribute.StateEnabled }, new ColorDrawable(colorDisabled));
drawable.AddState(new int[] { }, new ColorDrawable(colorOff));
swtch_EnableEdit.ThumbDrawable = drawable;
Run Code Online (Sandbox Code Playgroud)
swtch_EnableEdit之前是这样定义的(Xamarin):
Switch swtch_EnableEdit = view.FindViewById<Switch>(Resource.Id.mySwitch);
Run Code Online (Sandbox Code Playgroud)
我没有设置所有填充,我不调用.setPadding(0,0,0,0).
最简单的方法是定义轨道色调,并将色调模式设置为 src_over 以移除 30% 的透明度。
android:trackTint="@drawable/toggle_style"
android:trackTintMode="src_over"
Run Code Online (Sandbox Code Playgroud)
切换样式.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/informationDefault"
android:state_checked="true"
/>
<item android:color="@color/textDisabled" android:state_checked="false"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
164363 次 |
| 最近记录: |