gpt4 book ai didi

android - SwitchCompat 在 Android 5.0 Lollipop 模拟器上不显示缩略图

转载 作者:太空宇宙 更新时间:2023-11-03 11:34:46 29 4
gpt4 key购买 nike

我读到了新的 SwitchCompat,它被引入以在 Android 5.0 中实现 Switch 小部件。我尝试使用相同的方法,但看不到下图所示的可绘制缩略图图像。

enter image description here

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

预览设计 enter image description here

这是我得到的异常

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.drawable.Drawable.getPadding(android.graphics.Rect)' on a null object reference

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

最佳答案

这是一个已知的 issue你应该提供拇指和轨迹:

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

你可以使用这个link对其进行自定义。

布局:

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

}

关于android - SwitchCompat 在 Android 5.0 Lollipop 模拟器上不显示缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27568560/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com