我的项目活动之一包括用 string.xml 编写的长文本,我从 textview 中的资源中添加了一些图像,因为每个短语之后都有图像,然后是文本短语,然后是图像等等,
我使用以下代码从字符串中获取图像:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView htmlTextView = (TextView) findViewById(R.id.day_tv);
htmlTextView.setText(Html.fromHtml(getString(R.string.day), new ImageGetter(), null));
}
private class ImageGetter implements Html.ImageGetter {
public Drawable getDrawable(String source) {
int id = 0;
if (source.equals("image1.jpg")) {
id = R.drawable.a;
} else if (source.equals("image2.jpg")) {
id = R.drawable.b;
} else if (source.equals("image3.jpg")) {
id = R.drawable.c;
} else if (source.equals("image4.jpg")) {
id = R.drawable.d;
} else if (source.equals("image5.jpg")) {
id = R.drawable.e;
}
Drawable d = getResources().getDrawable(id);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
}
并在 string.xml 中编写 html img 标签:
<img src="image1.jpg">
我希望能够通过更改其宽度和高度来自定义每个图像,或者将其引用为可绘制样式以在图像周围添加边框。
我尝试使用波纹管代码敌人改变宽度和高度:
<img src="image1.jpg" width="100" height="150">
但它不起作用。
任何实现这一目标的建议将不胜感激,谢谢。