【问题标题】:How to remove the space before the first letter in a TextView如何删除TextView中第一个字母之前的空格
【发布时间】:2020-11-05 09:59:09
【问题描述】:

所以我有 3 个垂直对齐的 TextView。第二个字体更大。

现在,如果第二个 TextView 包含一个以 e.g. 开头的字符串一个 'B' 看起来视图是缩进的:

这是我的 XML 布局:

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView style="@style/TextAppearance.LabelSmall"
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView1" />

        <TextView style="@style/TextAppearance.LabelBig"
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="B TextView2" />

        <TextView style="@style/TextAppearance.LabelSmall"
            android:id="@+id/text3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView3" />
    </LinearLayout>

这里是对应的styles.xml文件:

<resources>
    <style name="TextAppearance.LabelBig" parent="TextAppearance.MaterialComponents.Headline3">
        <item name="fontFamily">@font/opensans_regular</item>
        <item name="android:fontFamily">@font/opensans_regular</item>
        <item name="android:textSize">48sp</item>
    </style>

    <style name="TextAppearance.LabelSmall" parent="TextAppearance.MaterialComponents.Body1">
        <item name="fontFamily">@font/opensans_regular</item>
        <item name="android:fontFamily">@font/opensans_regular</item>
        <item name="android:textSize">16sp</item>
    </style>
</ressources>

有没有办法让这些 TextView 看起来同样缩进?

感谢您的帮助。

【问题讨论】:

  • 你试过 android:includeFontPadding="false"
  • @PankajKumar 我做到了。它什么也没改变。同样it seems likeincludeFontPadding 只涉及文本height
  • 看来你是对的
  • 实际上问题出在排版上,为其他文本设置了像素,例如T 和 B 开始索引是不同的,所以如果你使用 T 和 48sp 你会注意到没有空格,所以如果你知道在这种情况下文本是硬编码的,你可以使用 android:paddingStart="-3dp"
  • 您是否尝试在“较小”TextViews 的开头添加边距?

标签: android android-layout kotlin textview android-xml


【解决方案1】:

您不需要为单行文本使用三个文本视图,只需使用 spannable。

在这里找到一些信息:https://medium.com/androiddevelopers/spantastic-text-styling-with-spans-17b0c16b4568

【讨论】:

  • 我没有尝试过,但我认为它不会真正解决我的问题,因为 'B's 之前的空白空间仍然存在。
  • 它将解决您的问题,因为当设置了 spannable 时,它​​们充当单个文本
猜你喜欢
  • 1970-01-01
  • 2021-07-27
  • 2021-03-28
  • 2016-04-23
  • 2016-04-26
  • 2021-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多