Switchcompat does not display a thumb image on the Android 5.0 lollipop simulator

I read about the new switchcompat introduced to implement the switch widget in Android 5.0. I try to use the same, but I can't see the paintable thumb image, as shown in the figure below

My XML code is as follows,

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

I can see the thumb image of the above layout in the preview design (graphical layout tab in eclipse), but when I run my code, I can't see the image

Preview design

This is the exception I get

Can anyone help solve the problem?

resolvent:

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

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

or

    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

Layout:

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

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

}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>