Android从strings.xml文件插入图像

时间:2014-07-07 16:39:58

标签: android xml string

大家好我需要在书中使用一些图片。

我的应用程序正在使用片段和ViewPager来显示文本,并且通过更改页面,它在同一TextView上显示新字符串,因此我无法使用ImageView在我想要的书籍的某些位置显示图像。

我发现可能的唯一方法是在字符串文件和文本之间定义它,但它现在还没有工作。它是我的字符串文件的一部分:

<string name="textch1"><b>CHAPTER ONE</b>
 \n\n<b>The Boy Who Lived</b>
 <img src="file:///android_asset/1.jpg"/>
 \n\nMr. and Mrs. Dursley, of number four, Privet Drive, were proud to say that they       were perfectly normal, thank you very much. They were the last people you’d expect to be   involved in anything strange or mysterious, because they just didn’t hold with such   nonsense.
 \n\nMr. Dursley was the director of a firm called Grunnings, which made drills. He was a big, beefy man with hardly any neck, although he did have a very large mustache. Mrs. Dursley was thin and blonde and had nearly twice the usual amount of neck, which came in very useful as she spent so much of her time craning over garden fences, spying on the neighbors. The Dursleys had a small son called Dudley and in their opinion there was no finer boy anywhere.
</string>

我的图片名称是1.jpg,它位于项目根目录的资源文件夹中。

谢谢你们

1 个答案:

答案 0 :(得分:0)

最简单的方法是使用WebView,它允许您使用html完美地格式化对象,还允许管理文本对齐,怀疑,字体,大小等。

尝试使用webview并按以下步骤操作。

WebView view=(WebView) findViewById(R.id.webViev);
        String data=new String();


        data="<html><head></head><body><b>CHAPTER ONE</b> \n\n<b>The Boy Who Lived</b><br/> <img src=\"file:///android_asset/photo.jpg\"  height=\"150\" width=\"150\"/><p>Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say that they       were perfectly normal, thank you very much. They were the last people you’d expect to be   involved in anything strange or mysterious, because they just didn’t hold with such   nonsense.Mr. Dursley was the director of a firm called Grunnings, which made drills. He was a big, beefy man with hardly any neck, although he did have a very large mustache. Mrs. Dursley was thin and blonde and had nearly twice the usual amount of neck, which came in very useful as she spent so much of her time craning over garden fences, spying on the neighbors. The Dursleys had a small son called Dudley and in their opinion there was no finer boy anywhere.</p></body></html>";

        view.loadDataWithBaseURL(null, data,"text/html", "utf-8",null); 

您可以在strings.xml文件中使用字符串数据。并且要加载的图像放在android项目的assets文件夹中。 jpg和png格式都适用于图像。 您可以使用baseUrL和historyURL。