java - 如何在没有ImageView的TextView中间添加图像到TextView

标签 java android image text textview

我想在相关信息下方添加公司位置的图像,但我还想在图像下方添加文本。我尝试了在互联网上找到的代码,但它的作用是图像位于左侧,文本位于右侧

代码如下:

    private TextView txt_contactos;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contactos);


    txt_contactos = (TextView) findViewById(R.id.txt_contacto);
    iv_Voltar = (ImageView) findViewById(R.id.iv_voltar);


    txt_contactos.setText("Pode contactar a empresa Juvex das seguintes formas:\n" +
                    "\n" +
                    "Morada: Estrada Nacional 8 – Edifício Vale de Canas, 2560 – 254, Vale de Canas, Torres Vedras\n" +
                    "\n" +
                    "Telefone: 261 334 540\n" +
                    "\n" +
                    "Fax: 261 315 437\n" +
                    "\n" +
                    "Email: geral@juvex.pt\n" +
            "\n" +
            "\n" +
            "\n" +
            "Ou pode contactar a empresa Juvex 3 das seguintes formas:\n" +
            "\n" +
            "\n" +
            "Morada: Praceta Gil Eanes nº 2, Parque Residencial do Almirante, 2660 – 444, Santo António dos Cavaleiros\n" +
            "\n" +
            "Telefone: 219 379 340\n" +
            "\n" +
            "Fax: 219 379 349\n" +
            "\n" +
            "Email: geral@juvex3.pt");

    txt_contactos.setCompoundDrawablesWithIntrinsicBounds(
            R.drawable.localizacaojuvex1, 0, 0, 0);

我尝试在最后、中间插入图像代码,但没有成功,甚至不显示图像。

提前谢谢您。

最佳答案

为此,您需要使用ImageSpan。我已经用 Kotlin 编写了代码,但推断到 Java 应该相当简单。类似于:
Kotlin

val textSpan = SpannableStringBuilder( "Your text string" )

// You will need to play with the size to figure out what works
val imageSize = context.resources.getDimension(R.dimen.your_text_size).toInt()

// Use AppCompatResources to get drawable for Android.M if required
val image = context.resources.getDrawable(R.drawable.your_image, null)
image.setBounds(0, 0, imageSize, imageSize)

val imageSpan = ImageSpan(image, ImageSpan.ALIGN_BOTTOM)

// This part is where you would have to do a little calculation to figure out the exact position you want
// to place the image at. I have given `positionToPlaceImageAt` just as a placeholder
textSpan.setSpan(imageSpan, positionToPlaceImageAt - 1, positionToPlaceImageAt, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)

yourTextView.text = textSpan

Java:

SpannableStringBuilder textSpan = new SpannableStringBuilder( "Your text string" );
int imageSize = (int) getBaseContext().getResources().getDimension(R.dimen.your_text_size);
Drawable image = getBaseContext().getDrawable(R.drawable.your_image);
image.setBounds(0, 0, imageSize, imageSize);
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
textSpan.setSpan(imageSpan, positionToPlaceImageAt - 1, positionToPlaceImageAt, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
yourTextView.setText(textSpan);

关于java - 如何在没有ImageView的TextView中间添加图像到TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60486683/

相关文章:

java - 有没有办法,我可以清空整个 JSONObject -- java

c# - 如果将回调分配给对象两次,事件是否会执行两次?

java - 打开 SSL TCP 套接字并发送 Web 请求

html - 调整手机网站的图片大小

Android:如何制作一个可点击的 map 图像,每个国家都有不同的 Action ?

java - 使用 JDBC 在 java 中设置用户身份验证

android - 通过 Intent 为一个 Activity 创建多个快捷方式

android - BroadcastReceiver 每次启动服务时接收

HTML 格式的 Android 应用程序

android - 如何在android中的图像上应用不同的颜色滤镜?