Android Switch Widget:设置android:track会导致拇指和曲目不显示

我无法自定义 Android Switch小部件的外观.我有自定义xml drawables,我想用于拇指(通常显示为On或Off的小按钮部分)和轨道(拇指滑过的背景).当我使用android:thumb设置拇指时,它工作正常.当我设置轨道时(无论是否设置了拇指),开关完全消失,我只剩下文字显示.

只需应用拇指,这是我的代码

<com.blahblahblah.blah.CustomSwitch
    android:id="@+id/switch_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOff="Off"
    android:textOn="On"
    android:text="Something Awesome"
    android:textColor="@android:color/black"
    android:thumb="@drawable/custom_switch_thumb" />

以下是预览窗口中的内容

并应用跟踪:

<com.blahblahblah.blah.CustomSwitch
    android:id="@+id/switch_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOff="Off"
    android:textOn="On"
    android:text="Something Awesome"
    android:textColor="@android:color/black"
    android:track="@color/track_color" />

应用了轨道的预览窗口:

作为参考,我在OSX 10.7.5上使用Android Studio 0.2.3.

解决方法

我偶然发现了同样的问题,并在 HoloEverywhere issue tracker上找到了解决办法.你需要设置< size>您正在使用的可绘制XML形状.

不工作(小部件在那里,我可以与它进行交互,但我看不到它,它是隐藏的):

<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">

  <solid android:color="@color/black_50" />
</shape>

工作(不是添加的< size>):

<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">

  <solid android:color="@color/black_50" />
  <size android:width="56dp" android:height="20dp" />
</shape>

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...