简体   繁体   English

安卓 如何在文字中插入图片

[英]Android. How to insert image in text

Actually I have TextView with some text. 其实我有一些文本的TextView。 I need to insert an image insight this text. 我需要在此文本中插入一个图像洞察力。 Please suggest the best way to do that. 请提出最佳方法。 Should be supported on different display sizes. 应该在不同的显示尺寸上得到支持。

Are you talking about to display image inside the TextView at either Top/Bottom/Right/Left side, if yes then: 您是否要在TextView的顶部/底部/右侧/左侧显示图像,如果是,则:

<TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello World, DemoExampleActivity!"
        android:drawableLeft="@drawable/icon"
        android:drawableRight="@drawable/icon"
        android:drawablePadding="10dip"/>

output: 输出:

在此处输入图片说明

You can set the image as the background for the TextView like this: 您可以像这样将图像设置为TextView的背景:

<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Text to be displayed"
    android:background ="@drawable/your_image"
    />

Good luck, Arkde 祝你好运,阿克德

If want to do it via xml 如果想通过xml来做

add this attribute to TextView element 将此属性添加到TextView元素

android:background="@drawable/icon"

If at runtime programatically 如果在运行时以编程方式

    TextView tv = (TextView)findViewById(R.id.textView);
    tv.setBackgroundDrawable(drawable);

            or

    tv..setBackgroundResource(resid);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM