How to remove TextView top margin?

2020-05-16 01:56发布

问题:

I do have a problem with TextView. I don't want to have any margin/padding above it.

<TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/ptp_hour"
          android:textColor="@color/black"
          android:textSize="100dp"
          android:height="100dp"
          android:layout_marginTop="0dp"
          android:paddingTop="0dp"
          android:includeFontPadding="false"
          android:maxLines="1"
          android:text="10"/>

My TextView looks like this and despite the textSize and height are set to the same value, there is a space above font. It bothers me because I want to put another view relatively to the top of the font. Is this spacing included into font itself?

And another question: If I found out that margin 20dp from top and 7dp from bottom works perfectly on my device, can I rely that it will behave in a similar way on other screens? (these margins are for buttons)

回答1:

using android:includeFontPadding="false" helped me a lot in a similar situation.



回答2:

I had the same issue where setting android:includeFontPadding=false did not help. The best solution I could find in reasonable time was to override the TextView's onDraw method and to adjust the canvas for the difference between the font metrics' top and ascent values:

FontMetricsInt fontMetricsInt;
@Override
protected void onDraw(Canvas canvas) {
    if (adjustTopForAscent){
        if (fontMetricsInt == null){
            fontMetricsInt = new FontMetricsInt();
            getPaint().getFontMetricsInt(fontMetricsInt);
        }
        canvas.translate(0, fontMetricsInt.top - fontMetricsInt.ascent);
    }
    super.onDraw(canvas);
}


回答3:

What you need to do is to put the other view relative to the top of the font, and give it a negative android:layout_marginBottom in dip, such that it matches the top of the font. If the font has a margin, I don't think there is a better way of doing it.



回答4:

Yes this space included by default. You are not able to remove that space as per my search area. So you need to have to implement some logic to have such view.

See below Image:

Its not a good idea but you can do like below code:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:background="#ffffff">
    <TextView android:layout_width="wrap_content"           
        android:layout_height="wrap_content"           
        android:id="@+id/ptp_hour"           
        android:textColor="@android:color/black"           
        android:textSize="100sp"
        android:lineSpacingMultiplier="0.8"
        android:scrollY="-100dp"
        android:scrollX="100dp"
        android:text="10"/>
    <TextView 
        android:text="10"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textColor="#00FFFFFF"
        android:shadowColor="#000000"
        android:shadowDy="-50"
        android:shadowRadius="1"
        android:textSize="100dp"/>

</LinearLayout>

Here, first "10" is of your properties and second one is as i have set for you.

Enjoy. :))