ToggleButton形状不变

问题描述:

我试图制作一个绿色背景的圆形切换按钮。我用ToggleButton形状不变

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval" 
    android:visible="true"> 
    <solid android:color="@color/material_green_A200" /> 
</shape> 

用于限定形状,然后使用

android:background="@drawable/circlebutton" 

改变的形状和颜色,但没有改变。它显示了一个没有任何背景颜色或形状,但文字是可见

+0

你能尝试只是把一个颜色? – 2014-11-21 15:52:04

+0

我做了,它显示的颜色,由于某种原因,它甚至显示在Android工作室的圆形按钮,但在我的手机上它不 – user3553551 2014-11-21 16:28:04

试试这个,

你activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ToggleButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/style_toggle_button" /> 

</RelativeLayout> 

风格切换按钮(style_toggle_button.xml):

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

    <item android:drawable="@drawable/offcirclebutton" android:state_enabled="false"/> 
    <item android:drawable="@drawable/oncirclebutton" android:state_pressed="true"/> 
    <item android:drawable="@drawable/oncirclebutton" android:state_checked="true"/> 
    <item android:drawable="@drawable/oncirclebutton" android:state_focused="true"/> 
    <item android:drawable="@drawable/offcirclebutton"/> 
</selector> 

样式OFF(offcirclebutton.xml):

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval" 
    android:visible="true"> 
    <solid android:color="@android:color/holo_orange_dark" /> 
</shape> 

风格ON(oncirclebutton.xml):

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval" 
    android:visible="true"> 
    <solid android:color="@android:color/holo_green_dark" /> 
</shape> 
+0

情况下,你看不到文字,设置textOn和textOff为空。像这样: android:textOn =“” android:textOff =“” – 2014-11-21 16:30:22

+0

我的技术有什么问题导致您的方法奏效? – user3553551 2014-11-21 16:36:32

+0

不知道它的颜色是如何设置的(“@ color/material_green_A200”)。根据其状态,此解决方案应用于切换按钮中的“系统”颜色... – 2014-11-21 16:45:13