切换拇指可以在选择器中的state_enabled设置

发布于 2025-02-13 17:17:08 字数 1011 浏览 1 评论 0原文

我正在用自己的拇指和轨道进行自定义开关。我想要的是,在我的拇指XML中,在检查状态下设置了不同的颜色,并且在禁用开关时,还设置了不同的颜色。问题在于,当我将state_enabled设置为false,Nothind发生时,只有检查状态起作用时,为什么会发生这种情况?

<?xml version="1.0" encoding ="utf-8"?>
<selector xmlns:android.....>
   <item android:state_checked = "true">
      <shape android:shape = "oval">
         <size android:width = "24dp" android:height="24dp"/>
         <solid android:color="@color/green"/>
      </shape>
   </item>

   <item android:state_checked="false">
      <shape android:shape="oval">
          <size android:width = "24dp" android:height="24dp"/>
          <solid android:color="@color/red"/>
      </shape>
   </item>
   <item android:state_enabled="false">
       <shape android:shape="oval">
          <size android:width = "24dp" android:height="24dp"/>
          <solid android:color="@color/black"/>
       </shape>
   </item>
</selector>

I'm making a custom switch with my own thumb and track. What I want is, in my thumb xml, set a different color in checked state, unchecked state and also, set a different color when the switch is disabled. The problem is that when I set state_enabled to false, nothind happen, just checked states works, why is this happening?

<?xml version="1.0" encoding ="utf-8"?>
<selector xmlns:android.....>
   <item android:state_checked = "true">
      <shape android:shape = "oval">
         <size android:width = "24dp" android:height="24dp"/>
         <solid android:color="@color/green"/>
      </shape>
   </item>

   <item android:state_checked="false">
      <shape android:shape="oval">
          <size android:width = "24dp" android:height="24dp"/>
          <solid android:color="@color/red"/>
      </shape>
   </item>
   <item android:state_enabled="false">
       <shape android:shape="oval">
          <size android:width = "24dp" android:height="24dp"/>
          <solid android:color="@color/black"/>
       </shape>
   </item>
</selector>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝對不後悔。 2025-02-20 17:17:08
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_enabled="true" android:state_selected="true">
<shape android:shape = "oval">
         <size android:width = "24dp" android:height="24dp"/>
         <solid android:color="@color/green"/>
      </shape>
    </item>

<item android:state_enabled="true" android:state_selected="false">
<shape android:shape="oval">
          <size android:width = "24dp" android:height="24dp"/>
          <solid android:color="@color/red"/>
      </shape>
</item>
<item android:state_enabled="false">
 <shape android:shape="oval">
          <size android:width = "24dp" android:height="24dp"/>
          <solid android:color="@color/black"/>
       </shape>
</item>

</selector>

尝试一下。此链接中有类似的解决方案。
https://stackoverflow.com/a/a/1238390/6183169

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

<item android:state_enabled="true" android:state_selected="true">
<shape android:shape = "oval">
         <size android:width = "24dp" android:height="24dp"/>
         <solid android:color="@color/green"/>
      </shape>
    </item>

<item android:state_enabled="true" android:state_selected="false">
<shape android:shape="oval">
          <size android:width = "24dp" android:height="24dp"/>
          <solid android:color="@color/red"/>
      </shape>
</item>
<item android:state_enabled="false">
 <shape android:shape="oval">
          <size android:width = "24dp" android:height="24dp"/>
          <solid android:color="@color/black"/>
       </shape>
</item>

</selector>

Try this. There is similar solution in this link.
https://stackoverflow.com/a/12383390/6183169

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文