Как сделать чтобы background в TextView не искажался а был по центру

Рейтинг: 0Ответов: 1Опубликовано: 10.03.2023

введите сюда описание изображения

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/view0"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">


    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/b1"
        android:gravity="center"
        android:text="Hello World"
        android:textSize="30sp"
        android:textStyle="bold" />


</LinearLayout>

Ответы

▲ 0Принят

Вот например квадратный фон у TextView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view0"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:background="@drawable/b1"
        android:padding="15dp"
        android:text="Hello world"
        android:textColor="@color/white"
        android:textSize="12sp" />

</LinearLayout>

и такой drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="2dip"
        android:color="@color/black" />
    <solid android:color="@color/colorAccent" />
</shape>

и вот результат:

введите сюда описание изображения

Растягивает его у вас потому что оно пытается заполнить всего родителя, вы же поставили match_parent