1

In my app I set one TextView and I noticed that there were some spaces above and below the text, as in the image below.

enter image description here

But my expected result is this :-

enter image description here

I Googled lots and found this answer link, but nothing happens.

Updated :

<TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textSize="50dp"
              android:background="#484848"
              android:textColor="#fff"
              android:text="A"
              android:layout_centerInParent="true"/>

This is the tag I used for displaying text.

Updated :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rl"
    tools:context=".MainActivity" >


    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textSize="50dp"
              android:background="#484848"
              android:textColor="#fff"
              android:text="A"
              android:paddingTop="0dp"
              android:layout_centerInParent="true"/>


</RelativeLayout>

Updated Image :

enter image description here

Community
  • 1
  • 1

2 Answers2

1

Actually, its a padding of 9-patch backgrounds drawable which is by default for Android TextView background. And you are just set color to it #484848. So you don't have any solution for it.

As per my concern use the same size of height (android:layout_height) for your TextView as same as your TextSize (android:textSize="50dip") of your TextView. (instead of android:layout_height="wrap_content")

Try with below code:

<TextView android:layout_width="wrap_content"
              android:layout_height="50dip"
              android:layout_marginTop="-5dip"
              android:textSize="50dip"
              android:background="#484848"
              android:textColor="#fff"
              android:text="A"
              android:includeFontPadding="false"
              android:layout_centerInParent="true"/>

I also added some margin in negative. android:layout_marginTop="-5dip"

Hope this will help you a little bit.

user370305
  • 108,599
  • 23
  • 164
  • 151
0

android:layout_height="wrap_content" only works at certain times, for example, when you setHeight(), setWidth(), setText(), setMinHeight(), .... These methods will request the layout to wrap the content again.

But setTextSize() doesn't request the layout.

To trigger wrap_content to work again, you can call setMinHeight(0).

Fernando Matsumoto
  • 2,697
  • 1
  • 18
  • 24
Weidian Huang
  • 2,787
  • 2
  • 20
  • 29