问题
I want to use a Switch and change its Track Color. So in my opinion nothing spectacular.
My Switch layout:
<Switch
android:id="@+id/Switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:track="@color/gray"
android:textOn="@string/on"
android:textOff="@string/off"
android:text="@string/musiconoff" />
and my color "grey":
<color name="gray">#666666</color>
My Problem is that the Switch is shown as a 1-pixel Switch. It's just a small line. If I delete the "color"-line, the Switch is correct (without grey of course).
Where is my fault?
回答1:
Nothing worked for me except this
if (isChecked) {
mSwtPrivacyView.getTrackDrawable().setColorFilter(ContextCompat.getColor(this, R.color.switch_track_checked_true_color), PorterDuff.Mode.SRC_IN);
} else {
mSwtPrivacyView.getTrackDrawable().setColorFilter(ContextCompat.getColor(this, R.color.switch_track_checked_false_color), PorterDuff.Mode.SRC_IN);
}
回答2:
Following code solves the problem:
In you Activity/Fragment (XML):
<Switch
android:id="@+id/sMuteNotifications"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="1dp"
android:checked="true"
android:gravity="center_vertical"
android:switchMinWidth="56dp"
android:thumb="@drawable/bg_thumb" // Create this drawable
android:track="@drawable/bg_switch_states"/> // and this one too
bg_thumb :(Use this or any image you want)
Create this in your
drawables
folder
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="oval" xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@android:color/white"/>
<size android:width="28dp" android:height="28dp"/>
</shape>
bg_switch_states:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/bg_on_switch"/>
<item android:state_checked="false" android:drawable="@drawable/bg_off_switch"/>
</selector>
bg_on_switch :
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="18dip" />
<solid android:color="@color/colorPrimaryLight"/> // <---What ever color you want to use here
</shape>
bg_off_switch :
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="18dip" />
<solid android:color="@color/colorPrimaryDark"/> // <---What ever color you want to use here
</shape>
There you go...
Hope this will help someone 😎
回答3:
You might want to build a drawable with your grey color, like and place it in the res/drawable folder:
track.xml:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:width="48dp" android:height="22dp" />
<solid
android:color="#666666" />
<stroke
android:width="2dp"
android:color="#666666"
android:dashWidth="2dp"
android:dashGap="1dp" />
</shape>
Add whatever size you want to have your stroke(if needed) and add this drawable as the track property.
Does this help?
回答4:
Try this to change the track color of switch
android:track="@null"
android:background="@drawable/img_setings_toggle_on"
回答5:
android:track="@android:color/darker_gray"
Try this in your code
来源:https://stackoverflow.com/questions/26138949/switch-track-color