TextView Background Stretched

I want to set the background to a TextView , and I want the size of the TextView be the size of the background and text.

here is my code:

  <TextView android:id="@+id/attackeNameTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="13dp" android:paddingTop="8dp" android:paddingLeft="35dp" android:paddingRight="35dp" android:singleLine="true" android:ellipsize="end" android:textSize="12sp" android:text="thej hhekj hejk hjkesd ks dkl jsalkj dkl" android:background="@drawable/attackee_name_background"/> 

the result is that the text fits into the "fill" field just fine, but the background is stretched.

Do I need to assign a fixed layout_width TextView ?

+4
source share
2 answers

If you want the background to exactly fit your TextView size without stretching, and this text fits inside, you must use a 9-patch image or you need to set a fixed size equal to the size of the image. There are no other solutions.

Consider also that padding reduces only the area of โ€‹โ€‹the text field (the background does not shrink), and margin leaves space outside the TextView field.

So, if the background image (image_width, image_height) and you want to leave the registration inside the TextView without stretching the background and without using the 9-patch image, you should set the fixed size in the TextView to (image_width+paddingLeft+paddingRight, image_height+paddingTop+paddingBottom) .

+3
source

EDIT

here is a hacker way to do what you want ...

 <FrameLayout android:id="@+id/frameLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitCenter" android:src="@drawable/something" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="match_parent" android:text="TextView" /> </FrameLayout> 
+3
source

Source: https://habr.com/ru/post/1394362/


All Articles