3

我阅读了有关SwitchCompat在 Android 5.0 中实现 Switch 小部件的新功能。我尝试使用相同的方法,但我看不到可绘制的拇指图像,如下图所示。

在此处输入图像描述

我的 XML 代码如下,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp">

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/sampleSwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:showText="false"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:text="@string/action" />

    <TextView
        android:id="@+id/switchStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/sampleSwitch"
        android:layout_marginTop="22dp"
        android:text="@string/status"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

对于上述布局,我能够在预览设计(eclipse 中的图形布局选项卡)中看到拇指图像,但是当我运行我的代码时,我看不到图像。

预览设计 在此处输入图像描述

这是我得到的例外

java.lang.NullPointerException:尝试boolean android.graphics.drawable.Drawable.getPadding(android.graphics.Rect)在空对象引用上调用虚拟方法“”

请问有人可以帮忙解决问题吗?

4

2 回答 2

9

这是一个已知问题,您应该提供拇指和曲目:

    android:thumb="@drawable/thumb"
    android:track="@drawable/bg"

或者

    SwitchCompat switchCompat = (SwitchCompat)findViewById(R.id.sampleSwitch);
    switchCompat.setThumbResource(R.drawable.apptheme_switch_thumb_holo_light);
    switchCompat.setTrackResource(R.drawable.apptheme_switch_track_holo_light);

您可以使用此链接对其进行自定义。

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp">

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/sampleSwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:textOff="OFF"
        android:textOn="ON"
        android:text="Toggle Me"
        android:clickable="true"
        android:checked="true" />

</RelativeLayout>

和代码

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        SwitchCompat switchCompat = (SwitchCompat)findViewById(R.id.sampleSwitch);
        switchCompat.setThumbResource(R.drawable.apptheme_switch_thumb_holo_light);
        switchCompat.setTrackResource(R.drawable.apptheme_switch_track_holo_light);    
    }

}
于 2014-12-29T07:15:30.680 回答
5

这是 AppCompat https://code.google.com/p/android/issues/detail?id=78262上 drawable-hdpi 中文件损坏的错误

要修复它,只需用这两个文件覆盖它 https://github.com/lopespm/quick-fix-switchcompat-resources 将它添加到您的目录中 drawable-hdpi

XML

<android.support.v7.widget.SwitchCompat
android:id="@+id/dev_switch_show_dev_only"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

在 Java 上什么都不需要

于 2015-01-06T16:45:35.573 回答