2011-10-05 39 views
12

Tôi đã cố gắng thay đổi màu nền của nút chuyển đổi bằng cách sử dụng tệp XML làm màu trắng, nhưng nút chuyển đổi hoàn toàn bị hỏng. Có vẻ như tất cả các nút được phủ bằng màu trắng.Cách thay đổi màu nền của nút bật tắt trên Android

Không có dấu hiệu BẬT hoặc TẮT trên nút bật tắt khi tôi đã thay đổi màu của nút chuyển đổi thành màu trắng. Có cách nào khác để thay đổi nền không làm hỏng chỉ báo của nút chuyển đổi không?

<ToggleButton android:id="@+id/togglebutton" 
       android:layout_width="100px" 
       android:layout_height="46px" 
       android:background="#ffffff" 
       android:layout_above ="@+id/save" 
       android:textOn="DAY" 
       android:textOff="NIGHT" /> 

Đây là cách mã XML của tôi tìm nút chuyển đổi.

+1

Hiển thị cho chúng tôi những gì bạn đã thử. Nói chung, bạn cần sử dụng [danh sách trạng thái màu] (http://developer.android.com/guide/topics/resources/color-list-resource.html) hoặc [danh sách trạng thái có thể vẽ] (http: // nhà phát triển. android.com/guide/topics/resources/drawable-resource.html#StateList) để làm những gì bạn mô tả. –

Trả lời

13

Vâng, có một cách để thay đổi nền như bạn muốn, nhưng bạn phải sử dụng một bộ chọn như thế này làm nền:

<selector 
    xmlns:android="http://schemas.android.com/apk/res/android" 
> 
<item 
    android:state_focused="true" 
    android:state_pressed="false" 
    android:drawable="@drawable/some_image" /> 
<item 
    android:state_focused="true" 
    android:state_pressed="true" 
    android:drawable="@drawable/some_other_image" /> 
<item 
    android:state_focused="false" 
    android:state_pressed="false" 
    android:drawable="@drawable/some_image1" /> 
<item 
    android:state_focused="false" 
    android:state_pressed="true" 
    android:drawable="@drawable/other_image" /> 
</selector> 

Đối @Drawable, vv (bạn có thể sử dụng một màu hoặc .. tạo ra một gradient Kiểm tra this để biết thêm thông tin về các gradient

+0

cảm ơn bạn về mã và hướng dẫn – padmi

-13

Khi bạn dịch ngược SystemUI.apk của bạn, bạn nên đến các tập tin sau đây: SystemUI/res/values ​​/ colors.xml

sau đó thay đổi dòng sau :

# ff000000 #ffffffff # 80000000 # ffadc1d6 #ffffffff # ffe6e6e6

+0

Phải có cách dễ dàng hơn để thực hiện việc này? –

+1

Không làm điều này. – ZirconCode

1

Làm theo cách này để làm ToogleButton của bạn có nền màu đỏ khi On và màu xanh lá cây khi OFF

Đầu tiên, tạo tooglebutton_selector.xml trong thư mục drawable

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/togglebutton_on" 
     android:state_checked="true" /> 
    <item android:drawable="@drawable/togglebutton_off" 
     android:state_checked="false" 
     /> 
</selector> 

Second, tạo togglebutton_on.xml trong thư mục drawable

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid 
     android:color="#ff0000" /> // red color 
</shape> 

Thứ ba, tạo togglebutton_off.xml trong thư mục drawable

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid 
     android:color="#00FF00" /> // green color 
</shape> 

Cuối cùng, trong ToggleButton bạn

<ToggleButton 
      android:id="@+id/btnMon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/tooglebutton_selector" //set background of ToggleButton to tooglebutton_selector 
      /> 
+0

Bạn có thể trợ giúp không? http://stackoverflow.com/questions/39772456/change-togglebutton-color – John

Các vấn đề liên quan