我正在使用一个标准的开关控制与全息。轻主题在ICS应用程序。
我想改变切换按钮的高亮或状态颜色,从标准的浅蓝色到绿色。
这应该很容易,但我似乎不知道该怎么做。
我正在使用一个标准的开关控制与全息。轻主题在ICS应用程序。
我想改变切换按钮的高亮或状态颜色,从标准的浅蓝色到绿色。
这应该很容易,但我似乎不知道该怎么做。
当前回答
从芳草中得到的解决方案对我很有效。 关于他的超空间问题,我解决了所有的填充到开关。
EDIT
按照要求,这是我的资料。
在布局文件中,我的开关是在一个线性布局和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>
因为我使用Xamarin / Monodroid (min. Android 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;
swtch_EnableEdit以前是这样定义的(Xamarin):
Switch swtch_EnableEdit = view.FindViewById<Switch>(Resource.Id.mySwitch);
我没有设置任何填充,也没有调用。setpadding (0,0,0,0)
其他回答
作为现有答案的补充:您可以在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>
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>
使用这些选择器自定义轨道和拇指色调:
<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"/>
请记住,如果你使用标准Switch和android命名空间的这些属性,它将只适用于API 23和以后,所以使用SwitchCompat与应用命名空间xmlns:app="http://schemas.android.com/apk/res-auto"作为通用解决方案。
结果:
可能有点晚了,但是对于开关按钮,toogle按钮不是答案,你必须改变开关xml参数中的drawable:
android:thumb="your drawable here"
创建自己的9-patch图像,并将其设置为切换按钮的背景。
http://radleymarx.com/2011/simple-guide-to-9-patch/
到目前为止,最好从AppCompat中使用SwitchCompat。v7图书馆。然后,您可以使用简单的样式来更改组件的颜色。
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>
参考:Android开发者博客
编辑:
它应该正确应用的方式是通过android:theme="@style/ theme。MyTheme” 这也可以应用到父样式,如EditTexts, RadioButtons, Switches, CheckBoxes和ProgressBars:
<style name="My.Widget.ProgressBar" parent="Widget.AppCompat.ProgressBar">
<style name="My.Widget.Checkbox" parent="Widget.AppCompat.CompoundButton.CheckBox">
在Android Lollipop和以上版本中,在你的主题样式中定义它:
<style name="BaseAppTheme" parent="Material.Theme">
...
<item name="android:colorControlActivated">@color/color_switch</item>
</style>