Centered image in TextView

I want to create a TextView with some images in the middle of a block of text. I did it using

Html.fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)

      

and using my own method getDrawable()

:

@Override
public Drawable getDrawable(String source){
     int id;
     if (source.equals("image.png")) {
         id = R.drawable.ic_launcher;
     } else {
         return null;
     }
     LevelListDrawable d = new LevelListDrawable();
     Drawable empty = getResources()
             .getDrawable(id);
     d.addLevel(0, 0, empty);
     d.setBounds(0, 0,
             empty.getIntrinsicWidth(),
             empty.getIntrinsicHeight());
     return d;
}

      

replace tags <img>

. However, the bottom edge of the loaded images is always aligned with the bottom of the text like so:

enter image description here

My question is, is there a way to vertically center these images?

I used HTML code:

title = Html.fromHtml("<img src=\"image.png\" align=\"middle\"/>", this, null);
desc = Html.fromHtml("Text <img src=\"image.png\" align=\"middle\"/> "
                + "text <img src=\"image.png\" align=\"middle\"/> blah blah blah blah blah<br>"
                + "Blah <img src=\"image.png\" align=\"middle\"/> blah blah", this, null);

      

+3


source to share


2 answers


Try to set the line-height

text to the height of the image. This will make the space used by the text as tall as the image and should vertically center the text.

I'm not sure how exactly to implement this the way you generate your html, but you can try something like this to add css:



desc = Html.fromHtml("<div style=\"line-height: 60px;\">Text <img src=\"image.png\" align=\"middle\"/> "
            + "text <img src=\"image.png\" align=\"middle\"/> blah blah blah blah blah<br>"
            + "Blah <img src=\"image.png\" align=\"middle\"/> blah blah</div>", this, null);

      

Of course, you will want to change 60px to the height of your image.

0


source


try it

desc = Html.fromHtml("Text <img src=\"image.png\"  style=\"vertical-align: middle;\" /> "
+ "text <img src=\"image.png\" style=\"vertical-align: middle;\" /> blah blah blah blah blah<br>"
+ "Blah <img src=\"image.png\" style=\"vertical-align: middle;\" /> blah blah", this, null);

      



This might solve your problem.

0


source







All Articles