Android 在文本视图中显示图像
我希望在文本视图中显示图像。我的图像保存在res/raw目录中。我尝试使用HTML.ImageGetter,但找不到完整的参考资料。尝试一下,这样您就不必破解TextView的使用。这将允许您考虑在源字符串中标记的任意数量的图像。它还将允许相邻图像,并允许空的前导或尾随文本字符串Android 在文本视图中显示图像,android,textview,Android,Textview,我希望在文本视图中显示图像。我的图像保存在res/raw目录中。我尝试使用HTML.ImageGetter,但找不到完整的参考资料。尝试一下,这样您就不必破解TextView的使用。这将允许您考虑在源字符串中标记的任意数量的图像。它还将允许相邻图像,并允许空的前导或尾随文本字符串 LinearLayout parent = //get parent layout String input = //get input LayoutInflater inflater = this.getLayout
LinearLayout parent = //get parent layout
String input = //get input
LayoutInflater inflater = this.getLayoutInflater();
int imageIndex = input.indexOf("<image tag begin");
int endIndex = -1; //find index one past the end of the image tag
while (imageIndex > -1){
TextView tv = inflater.inflate(R.layout.my_textview, null);
tv.setText(input.substring(0, imageIndex));
ImageView iv = inflater.inflate(R.layout.my_imageview, null);
String imgTag = input.substring(imageIndex,endIndex);
iv.setImageResource(-1); //or however you set your image from the tag
parent.addView(tv); parent.addView(iv);
input = input.substring(endIndex);
}
如果可以嵌入多个图像,则必须
动态创建图像视图和后续文本视图,并将它们添加到
相关布局的父级
您可以使用以下选项:setCompoundDrawableSwithinInstincBounds(int-left、int-top、int-right、int-bottom) 你可以对textview做同样的操作
导入android.app.Activity;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.widget.TextView;
public class TextImageActivity extends Activity {
int imageNumber = 1; //int to check which image is displayed
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tvText = (TextView) findViewById(R.id.text);
final String testContent = "<html><body><b>Test</b><i>Italic</i><br/>"
+ "<img src=\"icon.png\"/>This is like testing if this thing works" + "<img src=\"a.png\"/>" +
" in a more elaborate</body></html>";
tvText.setText(Html.fromHtml(testContent, imgGetter, null));
}
private ImageGetter imgGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable drawable = null;
if(imageNumber == 1) {
drawable = getResources().getDrawable(R.raw.icon);
++imageNumber;
} else drawable = getResources().getDrawable(R.raw.a);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
.getIntrinsicHeight());
return drawable;
}
};
}
导入android.graphics.drawable.drawable;
导入android.os.Bundle;
导入android.text.Html;
导入android.text.Html.ImageGetter;
导入android.widget.TextView;
公共类TextImageActivity扩展活动{
int imageNumber=1;//int检查显示的图像
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tvText=(TextView)findViewById(R.id.text);
最终字符串testContent=“TestItalic
”
+“这就像测试这个东西是否有效”+“+
“以更详细的方式”;
setText(Html.fromHtml(testContent,imgGetter,null));
}
私有ImageGetter imgGetter=新ImageGetter(){
公共可绘制getDrawable(字符串源){
Drawable Drawable=null;
如果(imageNumber==1){
drawable=getResources().getDrawable(R.raw.icon);
++图像编号;
}else drawable=getResources().getDrawable(R.raw.a);
setBounds(0,0,drawable.getIntrinsicWidth(),drawable
.getIntrinsicHeight());
回拉;
}
};
}
您为什么需要这个?Android中始终有一个ImageView UI组件为什么要将图像放在textview中。为什么不改为将textview包装在其他布局中(例如linearLayout),并将imageview添加为同级呢。这将使管理这两个部分的外观变得更容易。试试这个:你绝对不需要把它放在文本视图中。文本使用textview,图像使用imageview。只需根据需要动态设置ImageView的可见性/内容即可。向textview添加同级imageview将使布局更易于重用/阅读或修改/更改。使用View.setVisibility(View.GONE | View.VISIBLE)启用/禁用问题中的imageview。因此,您基本上有一些要显示的字符串,并且在该字符串中的某个位置编码了一个应内联显示的图像标记?您提出的答案令人印象深刻。在我的例子中,TextView来自SQLite数据库,如果我实现您的解决方案,维护数据库将变得非常复杂。Android开发了一个类HTML.ImageGetter。我相信在我的情况下使用它会很有用。它应该同样简单。我认为SQLiteDB基本上是唯一ID和字符串值之间的键值映射。字符串值类似于“texttextexttext”。您根本无法修改存储,只要在需要向用户显示时将该值解析为3个视图,或者,如果您想拆分数据库,只需将字符串的3个组成部分:前导、图像和尾随存储在同一个表中的不同列中。@Slothberry有多种可能1。“taxtexttext”2。“文本”3.文本。我不想限制自己需要插入图像的数量和位置。它应该放在任何地方,可以放多少就放多少。@Slothberry您的技术可以使用,但我们如何知道索引过去的图像以及如何设置位于本地目录中的图像。我找到了一个适合我的解决方案。我很欣赏你的技巧。请详细说明你的答案。如何使用它在TextView中插入多个图像。
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.widget.TextView;
public class TextImageActivity extends Activity {
int imageNumber = 1; //int to check which image is displayed
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tvText = (TextView) findViewById(R.id.text);
final String testContent = "<html><body><b>Test</b><i>Italic</i><br/>"
+ "<img src=\"icon.png\"/>This is like testing if this thing works" + "<img src=\"a.png\"/>" +
" in a more elaborate</body></html>";
tvText.setText(Html.fromHtml(testContent, imgGetter, null));
}
private ImageGetter imgGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable drawable = null;
if(imageNumber == 1) {
drawable = getResources().getDrawable(R.raw.icon);
++imageNumber;
} else drawable = getResources().getDrawable(R.raw.a);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
.getIntrinsicHeight());
return drawable;
}
};
}