2
我有一个LinearLayout中的按钮,我分配在drawableLeft XML这样的:更改绘制,并设置为drawableLeft
<Button android:id="@+id/button_news"
style="@style/main_button"
android:drawableLeft="@drawable/news"
android:text="@string/button_news" />
这工作正常,按钮看起来像预期。
我想改变图像编程,以显示可用的新闻数量。这是我的代码:
private void updateNewsButton()
{
// load the original bitmap and draw the number of news on it to show it
Paint paint = new Paint();
paint.setColor(Statics.COLOR_GENERAL_HIGHLIGHT);
paint.setTextAlign(Paint.Align.CENTER);
paint.setTextSize(24);
paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
paint.setAntiAlias(true);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.news).copy(Config.ARGB_8888, true);
Canvas canvas = new Canvas(bmp);
int count = 5; // TODO: load from database
canvas.drawText(Integer.toString(count), bmp.getWidth()/2, bmp.getHeight()/2, paint);
Drawable d = new BitmapDrawable(bmp);
_btnNews.setCompoundDrawables(d, null, null, null);
}
运行上面的代码后,我的按钮上的图像就消失了。我看不出我做错了什么,因为我之前使用过类似ImageView技术。除了最后两行之外,所有内容都是一样的。对于ImageView的我只是直接分配位图:
...
imageView.setImageBitmap(bmp);
我需要在这里使用drawableLeft,因为有一些其他bottons,所以我不能再使用ImageView的。
任何想法我的代码有什么问题?
您是否尝试过使用setCompoundDrawablesWithIntrinsicBounds? – dymmeh 2012-04-11 19:01:46
直到您的评论。现在有一张图像,但比以前小一些。我想我必须在设置drawable之前将边界设置为0。必须检查它....至此感谢。 – infero 2012-04-11 19:30:40
添加行'd.setBounds(0,0,0,0);'没有改变任何东西。任何其他值正面或负面也不会改变任何东西。 – infero 2012-04-11 19:38:46