【问题标题】:How to change background of switch in android ?如何更改android中开关的背景?
【发布时间】:2014-03-04 12:27:09
【问题描述】:

我正在尝试更改开关的背景。它正在改变,但问题是某些文本是可见的。

下面是我的代码

  <Switch
    android:id="@+id/switchsubsurf"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="68dp"
    android:padding="5dip"
    android:textOff="                "
    android:textOn="                 "
    android:thumb="@drawable/toggle_button"
    android:track="@drawable/toggle_bg" />

【问题讨论】:

  • 为什么这里有很大的空间 android:textOff=" " android:textOn=" "??
  • stackoverflow.com/questions/10118050/…看看这个链接..我认为这会有所帮助。
  • 试试 android:button="@null"
  • 嗨,我尝试了你指定的内容,但我仍然在 switch 中看到一些“0”、“1”。
  • toggle_button 和 toggle_bg 是自定义文件吗??

标签: android


【解决方案1】:

在 res 中创建一个抽屉文件夹

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

<item android:drawable="@drawable/toggle_on" android:state_checked="true" android:state_pressed="true"/>
<item android:drawable="@drawable/toggle_on" android:state_checked="true" android:state_focused="false"/>
<item android:drawable="@drawable/toggle_off" android:state_checked="false" android:state_pressed="true"/>
<item android:drawable="@drawable/toggle_off" android:state_checked="false" android:state_focused="false"/>

</selector>

然后在 xml 中执行此操作

<Switch
    android:id="@+id/saveLoginCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn=""
    android:textOff=""
    android:text=""
    android:background="@drawable/toggle_selector"
    android:thumb="@android:color/transparent"
    android:track="@android:color/transparent"
    /> 

【讨论】:

  • 然后在 xml 中使用
  • 赞成将现有拇指和轨道设置为透明。
【解决方案2】:

看看这个答案:

Use Android 4.0 styled toggle-button

您可以使用兼容 2.2 的切换按钮并将其风格化,使其看起来像一个开关,或者您想要的任何东西。只需换掉帖子中提供的 *.pngs。

不知道为什么 Android 可以改变按钮的外观和感觉,而放弃切换按钮,转而使用开关。无论如何,为了兼容性,我会坚持使用切换按钮,特别是如果你想改变它的外观和感觉。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 2019-11-14
    • 2012-06-26
    • 1970-01-01
    相关资源
    最近更新 更多