如何在textview中让文本之间有空格?

13
我需要根据Android屏幕边界制作类似于这样的东西。
 S             M             T              W               T              F               S

我正在这样做:
    <TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="45dp"
    android:text="S       M       T        W        T        F       S" />

这个不太正常,没有按照设备屏幕边界来展示。我需要像这样的效果:

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="45dp"
    android:text="S""10dp""M""10dp""T""10dp""W""10dp""T""10dp""F""10dp""S""10dp" />

所以你想填充设备屏幕吗?这个信件是固定的吗? - KOTIOS
6个回答

22

我们可以使用它。

<TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="A&#160;B&#160;&#160;&#160;&#160;&#160;C"/>

你能解释一下这实际上是做什么的吗? - Philipp Jahoda
1
 是非换行空格的实体编号。http://www.w3schools.com/html/html_entities.asp - App Kart
老兄,它确实给了空间,但不够,我需要给更多的空间,我已经将值从160改为1000,但没有起作用。 - Steve
实际上,我值之间需要的空间比这里显示的要多。当我提问时,我已经给了更多的空间,但是StackOverflow在这些值之间显示的空间较少。 - Steve
@Steve,将160更改为1000没有任何效果。这是不间断空格的实体编号,因此您应该多次复制@#160;。 - App Kart

9

使用letterSpacing属性来控制字母之间的间距:

android:id="@+id/txt_work"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:typeface="serif"
android:layout_below="@+id/txt_wecm"
android:layout_marginLeft="20dp"
android:textSize="32dp"
android:letterSpacing="0.1"
android:textColor="#fff"
android:text="World" 

它还在第一个字母之前和最后一个字母之后添加空格,但现在它符合我的需求。 - Dino Tw
很好,我已经点赞了,因为这是正确的方式。但是这只适用于API>20(API 21及以上)。 - sifr_dot_in

4

使用 Unicode 字符 NO-BREAK SPACETextView 支持此功能。

请尝试此操作。

<TextView
  android:id="@+id/textView1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="45dp"
  android:text="@string/days" />

把这个放到String.xml中。
<string name="days">S\u00A0M\u00A0T\u00A0W\u00A0T\u00A0F\u00A0S</string>

编辑 1

这个方法可以满足你的需求,但不是最高效的方式。如果你想试试只用一个TextView的解决方案,可以尝试以下方法:

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="S" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="M"
    android:textAlignment="center" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="T" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="W" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="T" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="F" />

<TextView
    android:id="@+id/textView7"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="S" />


你能否稍微解释一下吗? - Steve
嘿,这个可以满足你的要求,但不是最有效的方法。在你找到使用一个TextView的解决方案之前,你可以尝试像这样做。 - MilapTank

0
如果你想在textView中的文本之间增加一些空间,你可以使用android:lineSpacingExtra="10dp"。
你可以尝试这样做:
<TextView
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:lineSpacingExtra="10dp"
        android:text="hi
                     hello"/>

0

TextView 中的字母间距 -

android:textScaleX

设置文本的水平缩放因子。

必须是浮点值,例如“1.2”。

您可以根据字母之间所需的比例增加缩放因子。

Android 文档请参阅 android:textScaleX


1
这会拉伸文本,但不会在字符之间添加空格。 - Sandy D.

-1
textView.setText(Html.fromHtml("<span style=letter-spacing:10px>TEXT</span> "));

但我不确定这是否有效


TextView支持的标签列表 - http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html - My God

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接