How to align text at the top of a TextView?

How to align text at the top of a TextView?
Equivalent Android API for Swings setInsets ()?
that is, the start of the text should start at (0,0) TextView

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:textSize="42sp" android:paddingTop="0px" android:gravity="top"> </TextView> </LinearLayout> 

I used the snippet above, but still the output is not as expected
Any ideas?

+39
android layout
Nov 17 '09 at 7:36
source share
6 answers

Thus, the space at the top of the TextView is a complement used for characters outside the English language, such as accents. To remove this space, you can either set the android:includeFontPadding to false in your XML, or do it programmatically using the setIncludeFontPadding(false) function.

See the SDK documentation for TextView if this is not yet clear.

SEPARATE RESPONSE
If setting the android:includeFontPadding does not do what you are trying to do, another solution is to override the onDraw(Canvas canvas) method of the used TextView so that it removes the extra top add-on that Android adds to each TextView. After writing my initial answer, I noticed that for some reason, TextView includes an additional addition in addition to filling the font. Removing the indentation of the font, as well as adding an additional addition, aligns the text at the top of the TextView. Take a look at the code snippet below.

 public class TopAlignedTextView extends TextView { // Default constructor when inflating from XML file public TopAlignedTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } // Default constructor override public TopAlignedTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs); setIncludeFontPadding(false); //remove the font padding setGravity(getGravity() | Gravity.TOP); //make sure that the gravity is set to the top } /*This is where the magic happens*/ @Override protected void onDraw(Canvas canvas){ TextPaint textPaint = getPaint(); textPaint.setColor(getCurrentTextColor()); textPaint.drawableState = getDrawableState(); canvas.save(); //converts 5dip into pixels int additionalPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getContext().getResources().getDisplayMetrics()); //subtracts the additional padding from the top of the canvas that textview draws to in order to align it with the top. canvas.translate(0, -additionalPadding); if(getLayout() != null) getLayout().draw(canvas); canvas.restore(); } } 
+66
Jul 04 '11 at 15:39
source share

The actual font itself has extra spaces at the top and bottom that I consider.

Usually what I do gives negative view fields. The problem is that it looks bad when devices have different fonts.

 android:layout_marginTop="-5dp" android:layout_marginBottom="-5dp" 

You will need to adjust the font size, a larger font size will require more negative filling.

+18
Jul 18 '13 at 22:18
source share

In your .xml write that:

 android:gravity="center|top" 

The first "center" aligns the line horizontally in the center, and the next "top" aligns vertically up.

+16
Nov 20 '15 at 16:22
source share

You may not need to do your presentation based on TextView. Try expanding the look and writing text in onDraw() .

Have a look here: https://stackoverflow.com/a/464829/

+2
Dec 13 '16 at 11:09
source share

I think your fragment is right. I think you are concerned about some pixels between the top of the letter "T" and the top edge of the TextView? This space is necessary to display some letters that are not in the English alphabet.

Check out the links for an idea:

+1
Jan 14 '10 at 11:45
source share

I'm not sure if I understand what you mean, but the text inside the text view will be aligned at the top if you use gravity = top in the text view.

If you put the backgrund color in the text, you can see what happens ...

I assume the problem is that the textview is centered as you use fill_parent both in height and in width on the parent LinearLayout. The text view seems like an only child, so try to put gravity = top on LinearLayout ...

This is the only thing I could think of ...

0
Nov 27 '09 at 22:57
source share



All Articles