如何写在Android的代码对ImageView的文本? [英] How to write Text on ImageView in android coding?
问题描述
您好我一直在试图写号上ImageView的。对于N多的问题图像。如果用户输入的答案是正确的,那么它应该显示题号与错误的标记图像的刻度标记的图像别的题号。我需要写在图像上的问题数量。 这样的例子:<一href="http://hotelreservbyvic.comli.com/whatineed.jpg">http://hotelreservbyvic.comli.com/whatineed.jpg我的code是在这里:
Hi I've been trying to write Numbers on Imageview. Images for N number of questions. If the user entered answer is correct, then it should be displayed with question number with tick mark image else question number with wrong mark image. I need to write the question number on the image. The example of this: http://hotelreservbyvic.comli.com/whatineed.jpg My code is here:
LinearLayout l_layout = (LinearLayout) findViewById(R.id.linear_view_report);
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
ImageView[] imgview=new ImageView[questions.length];
for(int i=0;i<no_of_questions;i++)
{
if(userEnteredAnswers[i]==correct_answer[i]){
Bitmap bm=BitmapFactory.decodeResource(getResources(),R.drawable.correct);
Canvas canvas = new Canvas(bm);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(10);
canvas.drawText(i, 5, 5, paint);
imgview[i]=new ImageView(this);
imgview[i].setImageDrawable(new BitmapDrawable(bm));
l_layout.addView(imgview[i]);
}
else {
Bitmap bm=BitmapFactory.decodeResource(getResources(),R.drawable.wrong);
Canvas canvas = new Canvas(bm);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(10);
canvas.drawText(i, 5, 5, paint);
imgview[i]=new ImageView(this);
imgview[i].setImageDrawable(new BitmapDrawable(bm));
l_layout.addView(imgview[i]);
}
}
我得到这样的警告:
I get this warning:
The constructor BitmapDrawable(Bitmap) is deprecated
没有图片显示在运行时。 我究竟做错了什么?
There is no image show at run time. What am I doing wrong?
推荐答案
我相信最简单的解决方法没有覆盖任何东西将有一个的TextView
,并设置其背景的绘制
资源。
I believe the easiest workaround without overriding anything would be to have a TextView
and set its background with a drawable
resource.
例如:
TextView t = (TextView)findViewById(R.id.my_text_view);
// setting gravity to "center"
t.setGravity(Gravity.CENTER);
t.setBackgroundResource(R.drawable.my_drawable);
t.setText("FOO");
这篇关于如何写在Android的代码对ImageView的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!