2016-05-17 145 views

回答

3

我認爲你將不得不創建按鈕的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"?> 

<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"/> 

bg_selected和bg_normal就像9-補丁圖像。

See the Android developer guide on 9-patch images

0

類似的問題被打上一個重複開關,雖然是不一樣的東西切換按鈕,在Android系統。 How to change color of the toggle button? 兩者都從CompoundButton繼承;但有一些不同的方法和定製支持。 在別處發佈的setBackground無法更改拇指或邊框。下面是改變邊界的基礎上 How set background drawable programmatically in 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;如添加這使內轉綠色:

1

我認爲你可以使用主題,如果變化不大。

<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" /> 
0

它是一個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" /> 

在您的值 - 設置此樣式>樣式文件

<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>