2

我有这段代码用于在 ToggleButton 文本的右侧放置一个可绘制对象:

ToggleButton myToggle = (ToggleButton) v.findViewById(R.id.filtro_stat);
Drawable drawable = getResources().getDrawable(R.drawable.a010_ico);
drawable.setBounds(0, 0, (int) (drawable.getIntrinsicWidth() * 0.5),
            (int) (drawable.getIntrinsicHeight() * 0.5));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, Utils.dpToPixels(33,
            getActivity()), Utils.dpToPixels(26, getActivity()));

myToggle.setCompoundDrawables(null, null, sd.getDrawable(), null);
myToggle.setTextOn(getActivity().getString(R.string.string_on));
myToggle.setTextOff(getActivity().getString(R.string.string_off));

final int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    myToggle.setBackgroundDrawable(getResources().getDrawable(
            R.drawable.blue));
} else {
    myToggle.setBackground(getResources().getDrawable(
                R.drawable.blue));
}
myToggle.setChecked(false);

如果我在 Android 6 的设备上运行,drawable 不会出现,否则如果我在旧的 android 版本上运行代码,一切正常。我该如何解决?

4

1 回答 1

0

我有同样的问题,这就是我解决它的方法:

创建 de ScaleDrawable 后:

drawable = sd.getDrawable();

drawable.setBounds(0, 0, (int) (drawable.getIntrinsicWidth() * 0.5),
            (int) (drawable.getIntrinsicHeight() * 0.5));

myToggle.setCompoundDrawables(null, null, drawable, null);
于 2016-03-23T18:52:41.263 回答