Android 更改切换按钮的颜色
是否有一种方法可以通过编程方式更改切换按钮的切换颜色,而不是背景色 我在文档中没有看到对它的引用 在drawable文件夹中,创建一个xml文件my_btn_toggle.xml:Android 更改切换按钮的颜色,android,android-button,Android,Android Button,是否有一种方法可以通过编程方式更改切换按钮的切换颜色,而不是背景色 我在文档中没有看到对它的引用 在drawable文件夹中,创建一个xml文件my_btn_toggle.xml: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="false" android:drawable="@color/red" /> <it
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@color/red" />
<item android:state_checked="true" android:drawable="@color/green" />
</selector>
在xml部分定义切换按钮:
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton"
android:background="@drawable/my_btn_toggle"/>
最后但并非最不重要:)
在上面的切换按钮中使用下面的可绘制按钮
如何更改样式
styles.xml
<style name="ToggleButton" parent="Base.Widget.AppCompat.Button">
<item name="colorAccent">select your color</item>
<style>
选择你的颜色
现在在布局中的切换按钮上设置样式
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/ToggleButton"/>
[更改切换按钮的颜色][1]
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton"
android:textColor="#f26e08"
android:textSize="30dp"
android:padding="20dp"
android:background="@drawable/selector"
android:layout_gravity="center_horizontal" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton2"
android:textColor="#f26e08"
android:background="@drawable/selector"
android:textSize="30dp"
android:padding="20dp"
android:layout_gravity="center_horizontal" />
selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/toggleon"
android:state_checked="true"/>
<item
android:drawable="@drawable/toggleoff"
android:state_checked="false"/>
</selector>
toggleon.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke android:color="#0a8e9b"
android:width="6dp"/>
</shape>
</item>
<item android:bottom="6dp">
<shape android:shape="rectangle">
<solid
android:color="#b5f4f2"/>
</shape>
</item>
</layer-list>
toggleoff.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke android:color="#f26e08"
android:width="6dp"/>
</shape>
</item>
<item android:bottom="6dp">
<shape android:shape="rectangle">
<solid android:color="#56fd58"/>
</shape>
</item>
</layer-list>````
[1]: https://i.stack.imgur.com/R2oOV.png
selector.xml:
toggleon.xml
toggleoff.xml
````
[1]: https://i.stack.imgur.com/R2oOV.png
我可能不清楚,我不想更改背景颜色,我想更改切换按钮底部栏的颜色。我已经测试了您的建议,它会更改按钮的背景。我希望更改切换按钮底部栏的颜色。我在问题中加入了一幅图片,以帮助描述我希望改变的内容。@Shawn我终于明白你的意思了:)我再次编辑我的答案,这将有力地解决你的问题。我应该在my_btn_toggle XML文件中加入最后一个代码块吗?所有可绘制的错误都会消失,这些可绘制文件中应该包含什么?更改切换颜色似乎有些过分。此代码用于更改切换按钮的底部栏,因为我们无法更改底部栏的颜色,因为它是通过代码而不是xml生成的。
<style name="ToggleButton" parent="Base.Widget.AppCompat.Button">
<item name="colorAccent">select your color</item>
<style>
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/ToggleButton"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton"
android:textColor="#f26e08"
android:textSize="30dp"
android:padding="20dp"
android:background="@drawable/selector"
android:layout_gravity="center_horizontal" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton2"
android:textColor="#f26e08"
android:background="@drawable/selector"
android:textSize="30dp"
android:padding="20dp"
android:layout_gravity="center_horizontal" />
selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/toggleon"
android:state_checked="true"/>
<item
android:drawable="@drawable/toggleoff"
android:state_checked="false"/>
</selector>
toggleon.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke android:color="#0a8e9b"
android:width="6dp"/>
</shape>
</item>
<item android:bottom="6dp">
<shape android:shape="rectangle">
<solid
android:color="#b5f4f2"/>
</shape>
</item>
</layer-list>
toggleoff.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke android:color="#f26e08"
android:width="6dp"/>
</shape>
</item>
<item android:bottom="6dp">
<shape android:shape="rectangle">
<solid android:color="#56fd58"/>
</shape>
</item>
</layer-list>````
[1]: https://i.stack.imgur.com/R2oOV.png