简体   繁体   中英

Textview letter spacing issue

  • I am trying a display a single letter on a TextView .
  • Here TextView is having a background drawable with a border but TextView expands and shrinks based on that single letter and I don't want that.

在此处输入图像描述

My layout code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp">

<TextView
    android:id="@+id/txt_letter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/drawable_bg"
    android:fontFamily="@font/viga"
    android:padding="@dimen/padding_16dp"
    android:text="A"
    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

drawable_bg.xml :

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

    <stroke
        android:width="1dp"
        android:color="#9E9E9E" />

    <corners android:radius="4dp" />

    <padding
        android:bottom="8dp"
        android:left="20dp"
        android:right="20dp"
        android:top="8dp" />
</shape>

You're using wrap_content so the size is change according to the text. Simply use fixed dimension

<TextView
    android:id="@+id/txt_letter"
    android:layout_width="20dp"   // fixed size
    android:layout_height="20dp"  // fixed size
    android:background="@drawable/drawable_bg"
    android:fontFamily="@font/viga"
    android:padding="@dimen/padding_16dp"
    android:text="A"
    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM