How to center text vertically in narrow text view in Android?

I have a TextView that has a height 30px and a textsize 40px. Since the text is taller than the View , I want to display only the middle part of the text. Like this: enter image description here

But with android:gravity="center_vertical" I can only display the top of the text with the bottom off. enter image description here

And this is my code:

  <TextView android:layout_width="wrap_content" android:layout_height="30px" android:textSize="40px" android:text="ABCDEFG" android:gravity="center_vertical" /> 

Does anyone know how to do this? Thanks!

+4
source share
2 answers

Can't you just wrap it in a LinearView set to the appropriate size and adjust the allowable value in text form to about half (depending on the fill) the font size?

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="20dp" android:background="@android:color/white"> <TextView android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@android:color/black" android:text="Sliced in half" android:textSize="60dp" android:layout_marginTop="-30dp"/> </LinearLayout> 

Result:

http://i.stack.imgur.com/jEdxx.png

+2
source

I very much doubt that you can do this. All the options that I came across when working with TextView relate to when the text is smaller than the view. This is the first question I came across, and otherwise this is a requirement. One of the options I used earlier was android:scrollHorizontally="true" , where you indicate whether the text is allowed wider than the view.

Interest Ask. It would be great if you could add the code.

+1
source

All Articles