[英]How to change indicator color in Toggle Button ( Android)
我认为您将必须创建2个按钮的自定义图像,然后使用可绘制的xml作为背景。
例如:
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btntoggle_selector"
android:textColor="@android:color/white"
android:textOff="Toggle Button OFF"
android:textOn="Toggle Button ON "
android:onClick="onToggleButtonClick" />
然后btntoggle_selector xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_selected" android:state_checked="true" android:state_pressed="true"/>
<item android:drawable="@drawable/bg_selected" android:state_checked="true" android:state_focused="false"/>
<item android:drawable="@drawable/bg_normal" android:state_checked="false" android:state_pressed="true"/>
<item android:drawable="@drawable/bg_normal" android:state_checked="false" android:state_focused="false"/>
</selector>
bg_selected和bg_normal只是9补丁之类的图像。
如果变化不大,我认为您可以使用主题。
<style name="AppTheme.ToggleButton" parent="Base.Widget.AppCompat.Button">
<item name="colorButtonNormal">@color/colorPrimary</item>
<item name="android:textColor">@android:color/white</item>
<item name="colorAccent">@android:color/white</item>
</style>
在这里,您可以在ToggleButton上使用此新主题。
<ToggleButton
android:id="@+id/keyboard_caps_lock_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:minWidth="0dp"
android:textOff="@string/keyboard_caps_lock"
android:textOn="@string/keyboard_caps_lock"
android:textAllCaps="false"
android:theme="@style/AppTheme.ToggleButton" />
尽管Switch在Android中不是ToggleButton,但类似的问题被标记为重复。 如何更改切换按钮的颜色? 两者都继承自CompoundButton
; 但有一些不同的方法和自定义支持。 张贴在其他位置的setBackground
无法更改拇指或边框。 下面是一个示例,该示例基于如何在Android中以编程方式设置背景可绘制方式来更改边框的颜色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<stroke android:width="2px" android:color="#000000" /> </shape>
然后在代码中:
if(!isChecked) {
buttonView.setBackgroundResource(R.drawable.border_for_toggle_button);
}
因此,通过设置背景可以改变外观。 要更改内部的整个颜色,可以在上面添加更多可绘制的XML。 例如添加此内容以使内部变为绿色:
<gradient android:startColor="#FFFFFF"
android:endColor="#00FF00"
android:angle="270" />
这是一个XML文件
<Toggle Button
android:id="@+id/my_location"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:theme="@style/ToggleButton"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
在您的value-> style文件中设置此样式
<style name="ToggleButton" parent="Base.Widget.AppCompat.Button">
<item name="colorButtonNormal">@color/colorPrimary</item>
<item name="android:textColor">@android:color/white</item>
<item name="colorAccent">@android:color/white</item>
</style>
当我在上面使用这个答案时:
<style name="AppTheme.ToggleButton" parent="Base.Widget.AppCompat.Button">
<item name="colorButtonNormal">@color/red</item>
<item name="colorAccent">@color/green</item>
</style>
我的整个切换按钮背景变成红色。
如果您只想更改底部边框颜色,则可以改用:
<style name="AppTheme.ToggleButton" parent="Base.Widget.AppCompat.Button">
<item name="colorControlNormal">@color/red</item>
<item name="colorAccent">@color/green</item>
</style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.