我想在相关信息下方添加公司位置的图像,但我还想在图像下方添加文本。我尝试了在互联网上找到的代码,但它的作用是图像位于左侧,文本位于右侧
代码如下:
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/