Создайте ровную границу вокруг TextView с 1 ​​символом - PullRequest
0 голосов
/ 28 ноября 2018

Desired implementation

Здравствуйте, я хочу воссоздать этот "дизайн" в представлении Android.В основном это ровная граница вокруг TextView.

Я создал TextView:

            <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="R"
            android:textSize="30sp"
            android:background="@drawable/border_red"
            android:gravity="center" />

И добавил рисованную фигуру:

<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="@android:color/transparent"/>
    <stroke
        android:width="4dp"
        android:color="#ff687b" />
    <corners android:radius="4dp"/>
    <padding android:right="0dp" android:left="0dp" android:bottom="0dp" android:top="0dp"/>
</shape>

Странное поведение в программе предварительного просмотра Android и в приложении.Поведение:

enter image description here

Я еще не определил отступы.Все они установлены на 0dp, но у границы, похоже, уже есть какое-то поле / отступы.

Такое поведение затрудняет создание ровной прямоугольной рамки вокруг символа.: - (

В чем проблема, что я делаю не так?

Ответы [ 2 ]

0 голосов
/ 29 ноября 2018

Если вы не одобряете это, это нормально.Я показываю вам, чтобы вы учились.:)

Используя тот же XML, что и выше ...

Вот мой макет.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.vzw.www.myapplication.MainActivity"
    android:orientation="vertical"
    android:padding="10dp">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/red_border"
        android:padding="10dp">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:includeFontPadding="false"
            android:text="H"
            android:textStyle="bold"/>

    </LinearLayout>

</LinearLayout>

Вот результат для этого ...

enter image description here

Вы также можете достичь этого, создав собственный TextView ... но я не буду вдаваться в это ... но если вы хотите, вы могли бы.Это даже лучшее решение на самом деле.

0 голосов
/ 28 ноября 2018

Используйте android:includeFontPadding="false", чтобы удалить заполнение шрифта из TextView

<TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="R"
            android:textSize="30sp"
            android:includeFontPadding="false"
            android:background="@drawable/border_red"
            android:gravity="center" />

enter image description here

Это удалит большую часть paddingсверху и снизу TextView.

Чтобы получить идеальный квадрат, вы должны дать исправление width и height для TextView

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...