2

我有這個代碼把繪製的切換按鈕文本的權利:切換按鈕在Android上6:setCompoundDrawables不起作用

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的設備上運行,繪製不出現,否則,如果我在舊的Android版本上運行代碼,一切工作正常。我該如何解決它?

回答

0

我有同樣的問題,這是我如何固定它:

創建後去ScaleDrawable:

drawable = sd.getDrawable(); 

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

myToggle.setCompoundDrawables(null, null, drawable, null);