尽管代码是正确的,但是不能显示

时间:2013-08-14 19:12:21

标签: android drawable

我无法解决这个问题。如果你找到一个,请指出我。虽然textview正在处理点击,但drawable不是null但它没有显示图像

strengthtv = (TextView) findViewById(R.id.strengthtv);
strengthtv.setOnClickListener(strengthivListener);

performancetv = (TextView) findViewById(R.id.performancetv);
performancetv.setOnClickListener(performanceivListener);

shapetv = (TextView) findViewById(R.id.shapetv);
shapetv.setOnClickListener(shapeivListener);

...

switch (Prefs.PRODUCT) {
            case Prefs.APP_HC:
                Drawable strengthhc = resources.getDrawable(R.drawable.btn_goals_focus_bal_hc_2x_s);
                Drawable perfhc = resources.getDrawable(R.drawable.icon_goals_performance_hc_2);
                Drawable shapehc = resources.getDrawable(R.drawable.icon_goals_shape_hc_2x);

                Log.v("setgoals", " drawables------> " +  strengthhc + " -- "+ perfhc + " -- "+ shapehc);
                strengthtv.setCompoundDrawables(null, strengthhc, null, null);
                performancetv.setCompoundDrawables(null, perfhc, null, null);
                shapetv.setCompoundDrawables(null, shapehc, null, null);

                break;
            case Prefs.APP_BF:
                Drawable strengthbf = resources.getDrawable(R.drawable.btn_goals_focus_bal_bf_2x_s);
                Drawable perfbf = resources.getDrawable(R.drawable.icon_goals_performance_bf_2);
                Drawable shapebf = resources.getDrawable(R.drawable.icon_goals_shape_bf_2x);

                Log.v("setgoals", " drawables------> " +  strengthbf + " -- "+ perfbf + " -- "+ shapebf);
                strengthtv.setCompoundDrawables(null, strengthbf, null, null);
                performancetv.setCompoundDrawables(null, perfbf, null, null);
                shapetv.setCompoundDrawables(null, shapebf, null, null);

                break;
            }

1 个答案:

答案 0 :(得分:2)

看起来您可能想要使用setCompoundDrawablesWithIntrinsicBounds。或者,您需要在setBounds()上调用Drawable,然后才能在TextView

上进行设置

From the Docs

  

Drawables必须已经调用了setBounds(Rect)。