我有一个imageView图像,在图像上我想放置文本。我怎样才能做到这一点?Android图像上的文字
回答
您想使用FrameLayout或合并布局来实现此目的。 Android开发指南在这里有一个很好的例子:Android Layout Tricks #3: Optimize by merging。
有很多方法。你使用RelativeLayout或AbsoluteLayout。例如
使用亲属,例如,您可以让图像与父母的左侧对齐,并且还将文本与父母左对齐......然后,您可以在文本视图上使用边距和填充以及引力来获取它排列在你想要的图像上。
我来到这个方法意外地。但是当你想在imagebutton或imageview上放置一些文本时,它非常方便。示例 - 将当前温度放在当前条件的图像上。 – Phobos 2011-03-09 08:26:09
你可能
- 使从类的ImageView和
- 覆盖的方法
onDraw
继承一个新的类。先在该方法中调用super.onDraw()
,然后 - 然后绘制一些要显示的文本。
如果你这样做,你可以使用它作为一个布局组件,这使得它更容易与其他组件一起布局。
您可以使用一个TextView并改变其背景,要使用
这个答案的问题是你不能控制像imageView这样的背景属性。比例尺。 – 2013-09-08 12:25:39
这就是我做的形象和它的工作完全一样,你问一个RelativeLayout的内部:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myImageSouce" />
<TextView
android:id="@+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/myImageView"
android:layout_alignTop="@id/myImageView"
android:layout_alignRight="@id/myImageView"
android:layout_alignBottom="@id/myImageView"
android:layout_margin="1dp"
android:gravity="center"
android:text="Hello"
android:textColor="#000000" />
</RelativeLayout>
非常感谢这个例子。我试图使用compoundedDrawable textview,但我不能设置图像url。但是,在相对布局中使用此变通办法来识别layout_align设置很重要。 – AntonSack 2013-10-05 11:17:54
把控制的id到对齐解决了很多,谢谢 – 2017-10-15 00:25:44
你可能需要从不同势侧收取,如果:它似乎更容易有一个TextView与在背景上绘制:
<TextView
android:id="@+id/text"
android:background="@drawable/rounded_rectangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</TextView>
这个答案的问题是你不能控制像imageView这样的背景属性。比例尺。 – 2013-09-08 12:28:39
为此,您可以只用一个TextView的与android:drawableLeft/Right/Top/Bottom
将图像定位到TextView。此外,您还可以使用的TextView和绘制之间的一些填充有android:drawablePadding=""
使用方法如下:
<TextView
android:id="@+id/textAndImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableBottom="@drawable/yourDrawable"
android:drawablePadding="10dp"
android:text="Look at the drawable below"/>
有了这个,你不需要额外的ImageView的。也可以在TextView的多个边上使用两个drawable。
您将面临的唯一问题是,drawable不能缩放ImageView的方式。
尝试下面的代码,这将有助于you`
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/gallery1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#7ad7d7d7"
android:gravity="center"
android:text="Juneja Art Gallery"
android:textColor="#000000"
android:textSize="15sp"/>
</RelativeLayout>
下面的代码,这将帮助你
public class TextProperty {
private int heigt; //读入文本的行数
private String []context = new String[1024]; //存储读入的文本
/*
*@parameter wordNum
*
*/
public TextProperty(int wordNum ,InputStreamReader in) throws Exception {
int i=0;
BufferedReader br = new BufferedReader(in);
String s;
while((s=br.readLine())!=null){
if(s.length()>wordNum){
int k=0;
while(k+wordNum<=s.length()){
context[i++] = s.substring(k, k+wordNum);
k=k+wordNum;
}
context[i++] = s.substring(k,s.length());
}
else{
context[i++]=s;
}
}
this.heigt = i;
in.close();
br.close();
}
public int getHeigt() {
return heigt;
}
public String[] getContext() {
return context;
}
}
public class MainActivity extends AppCompatActivity {
private Button btn;
private ImageView iv;
private final int WORDNUM = 35; //转化成图片时 每行显示的字数
private final int WIDTH = 450; //设置图片的宽度
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.imageView);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int x=5,y=10;
try {
TextProperty tp = new TextProperty(WORDNUM, new InputStreamReader(getResources().getAssets().open("1.txt")));
Bitmap bitmap = Bitmap.createBitmap(WIDTH, 20*tp.getHeigt(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextAlign(Paint.Align.LEFT);
paint.setTextSize(20f);
String [] ss = tp.getContext();
for(int i=0;i<tp.getHeigt();i++){
canvas.drawText(ss[i], x, y, paint);
y=y+20;
}
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
String path = Environment.getExternalStorageDirectory() + "/image.png";
System.out.println(path);
FileOutputStream os = new FileOutputStream(new File(path));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
//Display the image on ImageView.
iv.setImageBitmap(bitmap);
iv.setBackgroundColor(Color.BLUE);
os.flush();
os.close();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
- 1. 在Android中的图像上写文字
- 2. Android中的图像文字
- 3. Android上的图像上的图像
- 4. 从图像上延伸出的图像上的文字绘制
- 5. 在图像上写文字
- 6. 在Android中上传的文件/图像
- 7. 围绕图像错误的Android文字
- 8. 如何刷卡图像+文字的Android
- 9. 带图像和文字的Android ImageButton
- 10. 在android中的图像中心文字?
- 11. ANDROID - 如何显示图像上的文字?
- 12. 将文字置于Android中的捕获图像上?
- 13. 试图在图像上显示文字
- 14. 梯度和图像上的文字
- 15. 有问题,把文字上的图像
- 16. 在WP7中的图像上写文字
- 17. 上面的两个图像文字
- 18. 悬停在图像上的文字
- 19. rtransparent div与图像上的文字
- 20. 图像上的填充文字
- 21. ios图像上的文字手势
- 22. RelativeLayout避免图像上的文字
- 23. 用上面的文字框住图像
- 24. 如何绘制图像上的文字
- 25. 图像上的文字定位错误
- 26. android上的图像问题
- 27. android上的图像问题
- 28. Android上的图像处理
- 29. android上的图像分割
- 30. alertdialog上的图像 - android
简单的方法是拿一个textView并且像你一样设置背景在ImageView中。它会让你的工作变得简单.. – Nepster 2014-05-23 12:08:05
只看下面的链接。 我希望这将有助于ü... https://stackoverflow.com/questions/11100428/add-text-to-image-in-android-programmatically/26064945#26064945 – 2014-09-26 17:19:14