2015-01-06 43 views
1

我已经试过以上以下如何样式ToggleButton,使背景颜色为白色?

View view = (View) findViewById (id); 
view.setBackgroundColor (Color.WHITE); 

破坏了切换按钮的财产。我再也看不出它是TextView还是ToggleButton。点击该按钮会产生onClick,但用户界面会发生变化,以显示所选的栏不存在。

ToggleButton btn = (ToggleButton) findViewById (id); 
btn.setBackgroundColor (Color.WHITE); 

这也会产生相同的错误。使用XML样式也会导致相同的错误。我正在使用Android Studio v1.0.2,目标是棒棒糖模拟器。我在4.4.4手机中尝试过同样的事情。我看到了同样的行为。

我不想使用图像。请不要向我暗示。我已经通过了stackoverflow中可用的线程 - 没有为我工作。

任何人都可以帮助我吗?

+0

你可以给出更多的细节或者你想让切换按钮看起来像在每个状态(开/关)的草图?您将需要创建一个选择器,并可能为每个状态绘制。请参阅:http://stackoverflow.com/a/1533412/2832027 – TTransmit

+0

感谢您的链接。该线程讨论使用图像进行检查/取消选中事件。我不想那样做。但只需设置切换按钮,使按钮颜色为白色。其他一切都保持不变。任何想法,如果这甚至可能? – user2731584

回答

2

它会像这样作为一个xml文件在可绘制的,你想要的切换按钮的背景。您需要将各种颜色添加到res/values中的颜色文件中。见:https://developer.android.com/samples/MediaRouter/res/values/colors.html

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_checked="true" android:state_pressed="true"> 
     <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
      <item android:top="2dp"> 
       <shape android:shape="rectangle"> 
        <solid android:color="@color/toggle_button_pressed" /> 
       </shape> 
      </item> 
      <item android:top="2dp" android:left="10dp" android:right="10dp"> 
       <shape android:shape="rectangle"> 
        <solid android:color="@color/toggle_button_checked" /> 
       </shape> 
      </item> 
      <item android:top="2dp" android:bottom="2dp" android:left="10dp" android:right="10dp"> 
       <shape android:shape="rectangle"> 
        <solid android:color="@color/toggle_button_pressed" /> 
       </shape> 
      </item> 
     </layer-list>  
    </item> 

    <item android:state_pressed="true"> 
     <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
      <item android:top="2dp"> 
       <shape android:shape="rectangle"> 
        <solid android:color="@color/toggle_button_pressed" /> 
       </shape> 
      </item> 
      <item android:top="2dp" android:left="10dp" android:right="10dp"> 
       <shape android:shape="rectangle"> 
        <solid android:color="@color/toggle_button_unchecked" /> 
       </shape> 
      </item> 
      <item android:top="2dp" android:bottom="2dp" android:left="10dp" android:right="10dp"> 
       <shape android:shape="rectangle"> 
        <solid android:color="@color/toggle_button_pressed" /> 
       </shape> 
      </item> 
     </layer-list>  
    </item> 

    <item android:state_checked="true"> 
     <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
      <item> 
       <shape android:shape="rectangle"> 
        <solid android:color="@color/toggle_button_shadow" /> 
       </shape> 
      </item> 
      <item android:bottom="2dp"> 
       <shape android:shape="rectangle"> 
        <solid android:color="@color/toggle_button_unpressed" /> 
       </shape> 
      </item> 
      <item android:bottom="2dp" android:left="10dp" android:right="10dp"> 
       <shape android:shape="rectangle"> 
        <solid android:color="@color/toggle_button_checked" /> 
       </shape> 
      </item> 
      <item android:bottom="4dp" android:left="10dp" android:right="10dp"> 
       <shape android:shape="rectangle"> 
        <solid android:color="@color/toggle_button_unpressed" /> 
       </shape> 
      </item> 
     </layer-list> 
    </item> 

    <item> 
     <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
      <item> 
       <shape android:shape="rectangle"> 
        <solid android:color="@color/toggle_button_shadow" /> 
       </shape> 
      </item> 
      <item android:bottom="2dp"> 
       <shape android:shape="rectangle"> 
        <solid android:color="@color/toggle_button_unpressed" /> 
       </shape> 
      </item> 
      <item android:bottom="2dp" android:left="10dp" android:right="10dp"> 
       <shape android:shape="rectangle"> 
        <solid android:color="@color/toggle_button_unchecked" /> 
       </shape> 
      </item> 
      <item android:bottom="4dp" android:left="10dp" android:right="10dp"> 
       <shape android:shape="rectangle"> 
        <solid android:color="@color/toggle_button_unpressed" /> 
       </shape> 
      </item> 
     </layer-list> 
    </item> 

</selector> 

我还没有检查过,所以我不知道它是否有效。如果我犯了任何错误,请告诉我。

+0

非常感谢您的支持!我可以看到,XML的作品:) – user2731584

+0

即使这个工作,我必须承认我不明白发生了什么。您能否详细说明层列表的工作方式?例如,如果我想中风,我该怎么做? – user2731584

+1

在图层列表中,列表最靠后的项目显示在最上面。它显示了几个矩形在彼此之上。 Android的drawables有点棘手,就好像你指定了一个高度和宽度的“size”,你只能用drawable来设置这个大小。这就是为什么我通过偏离视图边缘的偏移量来抵消矩形形状,比如'android:bottom =“4dp”'。要添加一个笔画:(i)在彼此的顶部绘制两个矩形,第一个是笔触颜色和第二个填充颜色,(ii)将笔触属性添加到形状,以便一直绘制笔画它。 – TTransmit