gpt4 book ai didi

android - android中的自定义切换按钮

转载 作者:行者123 更新时间:2023-11-30 03:42:34 24 4
gpt4 key购买 nike

我添加了一个自定义切换按钮,它工作正常。问题是切换按钮启用和禁用。我需要不同的按钮布局,按钮关闭,按钮打开和禁用,按钮打开和启用,按钮关闭和禁用以及按钮关闭和启用。这是开/关的代码。怎么办??

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<bitmap android:src="@drawable/button_pause_pressed"
android:gravity="center_vertical|center_horizontal" />
</item>
<item android:state_checked="false">
<bitmap android:src="@drawable/button_play_pressed_new"
android:gravity="center_vertical|center_horizontal" />
</item>
</selector>

最佳答案

我以前没有这样做,但由于 StateList 是可绘制的,因此可以引用另一个包含 选择器 的 XML 文件。

这看起来像这样......
按钮.xml:

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

<item android:state_enabled="true"
android:drawable="@drawable/button_enabled"/>

<item android:drawable=@drawable/button_disabled/>
</selector>


button_enabled.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<bitmap android:src="@drawable/button_pause_pressed"
android:gravity="center_vertical|center_horizontal" />
</item>
<item android:state_checked="false">
<bitmap android:src="@drawable/button_play_pressed_new"
android:gravity="center_vertical|center_horizontal" />
</item>
</selector>

button_disabled.xml 看起来像 button_enabled.xml,但包含禁用按钮的外观..

关于android - android中的自定义切换按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15505894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com