[英]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.