Reputation: 15676
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="90px"
android:layout_height="20px">
<TextView
android:maxLines="1"
android:ellipsize="end"
android:textSize="15px"
android:textColor="@android:color/white"
android:background="@color/material_grey_600"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="hello this is some long text" />
</LinearLayout>
This code successfully vertically aligns the text in the text view. The font's size is 75% of the height of the view.
However, if I increase the size of the text to, say, 19px or 95% of the height of the view, the vertical centering breaks because it seems that the text view won't draw the text any higher than the top of the view.
Is it possible to shunt the text up?
Upvotes: 2
Views: 747
Reputation: 15676
I drew the background separately using Canvas.DrawLine
and moved the TextView
up by 10% of the height of the background.
Upvotes: 0
Reputation: 1111
You can set the height to wrap_content
then use lineSpacingMultiplier
to set the line height to be 1.3
times higher than the text height. This will mean that the text is always approximately 75% of the line height.
Additionally you should use sp
instead of dp
for text sizes so that when people change the size of text in their settings the size of text in your app updates, this is important for accessibility.
Finally you don't need to wrap your TextView
in a LinearLayout
unless you plan to add more views
Upvotes: 1