Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Switch小部件:设置Android:track会导致thumb和track不显示_Android_Xml - Fatal编程技术网

Android Switch小部件:设置Android:track会导致thumb和track不显示

Android Switch小部件:设置Android:track会导致thumb和track不显示,android,xml,Android,Xml,我很难定制Android Switch小部件的外观。我有定制的xml绘图工具,我想用于拇指(通常表示开或关的小按钮部分)和轨迹(拇指滑动的背景)。当我使用android:thumb设置拇指时,效果很好。当我设置轨迹时(无论是否设置了拇指),开关将完全消失,只剩下显示的文本 以下是仅应用拇指时的代码: <com.blahblahblah.blah.CustomSwitch android:id="@+id/switch_1" android:layout_width="wra

我很难定制Android Switch小部件的外观。我有定制的xml绘图工具,我想用于拇指(通常表示开或关的小按钮部分)和轨迹(拇指滑动的背景)。当我使用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" />

应用轨迹的预览窗口:

<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。

一般来说,只是为了更改图像,您不需要对控件进行子类化,您可以对其进行样式设置,例如使用style属性


关于您的问题,有几个控件需要可绘制的颜色,而不是普通的颜色。在控件定义(xml部分)中,您可以定义允许引用,但不能将其限制为可绘制引用。所以要避免直接使用颜色。如果您不想添加图像,也可以使用xml对其进行定义,但请注意,这并不是在所有情况下都有效。

我只是偶然发现了相同的问题,并找到了解决方法。您需要设置正在使用的可绘制XML形状的

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


工作(不是添加的
):



谢谢您的回复。我知道我不需要对控件进行子类化,但我正在为这个特定的应用程序进行全面的子类化。它不应该影响任何东西,尤其是在这一点上,因为我的子类只重写了只调用super(…)的构造函数,而不做任何其他事情。我试过一种可拉深的颜色。我还没有找到一个情况下,一种颜色不工作时,可拉丝是首选。它在所有其他情况下都是有效的。在这种特殊情况下,使用可绘制或颜色,两者都不起作用。它对拇指很好,但对赛道不行。我只需要增加一个高度来固定它。例如
<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>