Фон Android TextView - программирование
Подтвердить что ты не робот

Фон Android TextView

У меня есть дизайн, который требует фона, подобного изображению ниже, для номера справа. Есть ли какие-либо возможности в Android?

enter image description here

Мне нужно сделать 9 патч-изображений и установить его в качестве фона?

4b9b3361

Ответ 1

Сначала создайте фигуру с закругленными углами.

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/>
    <corners android:radius="5px"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" /> 
</shape>

Затем примените это как фон к вашим представлениям:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/rounded_edges">
    <TextView 
        android:id="@+id/mytext"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:text="blah blah blah blah"
        android:padding="6dip"
        android:textColor="#000000" />
</LinearLayout>

Возможно, вам придется немного подкорректировать. Вы даже можете отказаться от LinearLayout и установить android:background от TextView до @drawable/rounded_edges

Ответ 2

Сначала создайте ресурс.

<?xml version="1.0" encoding="utf-8"?>
<!--  res/drawable/rounded_textview.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
 <solid android:color="#cccccc"/>
    <corners
     android:bottomRightRadius="15dp"
     android:bottomLeftRadius="15dp"
  android:topLeftRadius="15dp"
  android:topRightRadius="15dp"/>
</shape>

Затем ссылайтесь на это в своем макете:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="5dip"
    android:gravity="center"
    android:background="@drawable/rounded_textview" />
</LinearLayout>

Один из хороших ссылок:

TextView с закругленными углами

Спасибо.

Ответ 3

Я знаю, что это старый вопрос, но для тех, кто борется с этим, я настоятельно рекомендую библиотеку ViewBadger