Bil*_*ong 6 android colors widget android-switch
我想使用Switch并更改其Track Color.所以在我看来没什么了不起的.
我的Switch布局:
<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" />
Run Code Online (Sandbox Code Playgroud)
我的颜色"灰色":
<color name="gray">#666666</color>
Run Code Online (Sandbox Code Playgroud)
我的问题是Switch显示为1像素的Switch.这只是一条小线.如果我删除"颜色"线,开关是正确的(当然没有灰色).
我的错在哪里?
sil*_*udo 12
除此之外,没有什么对我有用
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);
}
Run Code Online (Sandbox Code Playgroud)
小智 9
这是一个非常简单的解决方案,对我有用。只需使用颜色选择器设置 xml 属性thumbTint 和 trackTint 即可。Track 是背景组件,thumb 是圆形组件。
<com.google.android.material.switchmaterial.SwitchMaterial
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:thumbTint="@color/switch_thumb"
app:trackTint="@color/switch_track" />
Run Code Online (Sandbox Code Playgroud)
或者更好地将这些项目添加到您的应用程序主题中以应用于所有开关:
<item name="thumbTint">@color/switch_thumb</item>
<item name="trackTint">@color/switch_track</item>
Run Code Online (Sandbox Code Playgroud)
switch_thumb.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/[your desired color]" android:state_checked="true" />
<item android:color="@color/[your desired color]" android:state_checked="false" />
</selector>
Run Code Online (Sandbox Code Playgroud)
switch_track.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/[your desired color]" android:state_checked="true" />
<item android:color="@color/[your desired color]" android:state_checked="false" />
</selector>
Run Code Online (Sandbox Code Playgroud)
小智 5
以下代码解决了该问题:
在您的活动/片段(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
Run Code Online (Sandbox Code Playgroud)
bg_thumb :(使用此图像或您想要的任何图像)
在您的
drawables文件夹中创建
<?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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
你去...
希望这会帮助某人
| 归档时间: |
|
| 查看次数: |
8350 次 |
| 最近记录: |