SwitchCompat在Android 5.0棒棒糖模拟器上未显示拇指图像 [英] SwitchCompat not showing thumb image on Android 5.0 lollipop emulator

查看:140
本文介绍了SwitchCompat在Android 5.0棒棒糖模拟器上未显示拇指图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了有关新的 SwitchCompat 的信息,该新版本旨在在Android 5.0中实现Switch小部件。我尝试使用相同的图像,但无法看到可绘制的拇指图像,如下图所示。

I read about the new SwitchCompat that has been introduced to implement the Switch widget in Android 5.0. I tried using the same but I am not able to see the drawable thumb image as seen in below image.

我的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中的图形布局选项卡)中看到拇指图像布局,但是当我运行代码时,我看不到图像。

I am able to see the thumb image in preview design (graphical layout tab in eclipse) for the above layout but when I run my code I dont see the image.

预览设计

Preview design

这是我得到的例外情况


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

请问有人可以解决这个问题吗?

Please can someone help solve the problem?

推荐答案

这是已知的问题,您应该提供以下内容:

This is a known issue and you should provide the thumb and track:

    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);

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

you can use this link to customize it.

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



和代码



And the codes

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);    
    }

}

这篇关于SwitchCompat在Android 5.0棒棒糖模拟器上未显示拇指图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆