使用strings.xml中文本资源发现多个空格只显示一个空格

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nongminkouhao/article/details/81512974

先上图
效果图
strings.xml代码如下:

<resources>
    <string name="app_name">Test</string>
    <string name="app_text">类型:类型1\n         类型2\n         类型3</string>
    <string name="app_text1">类型:类型1\n   类型2\n   类型3</string>
    <string name="app_text2">类型:类型1\n&#12288;&#12288;&#160;类型2\n&#12288;&#12288;&#160;类型3</string>
</resources>

布局文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              tools:context="com.hj.test.test.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="问题:使用strings.xml中的资源发现多个空格只显示一个"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:textStyle="bold"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_text"
        android:textColor="@android:color/black"
        android:textSize="16sp"/>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="解决方案0:使用strings.xml中的文本资源,设置文本时候使用两个全角空格+1个半角空格"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:textStyle="bold"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_text1"
        android:textColor="@android:color/black"
        android:textSize="16sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="解决方案1:使用strings.xml中的文本资源,设置文本时候使用全角和半角空格对应的字符来拼装"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:textStyle="bold"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_text2"
        android:textColor="@android:color/black"
        android:textSize="16sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="解决方案2:直接在text设置文本,显示的空格数目(9个半角空格)和输入的空格数一样"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:textStyle="bold"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="类型:类型1\n         类型2\n         类型3"
        android:textColor="@android:color/black"
        android:textSize="16sp"/><!--半角空格-->


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="解决方案3:直接在布局文件xml中设置文本,使用两个全角空格+一个半角空格达到预期效果"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:textStyle="bold"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发型:小平头\n   毛寸\n   光头"
        android:textColor="@android:color/black"
        android:textSize="16sp"/><!--全角空格-->
</LinearLayout>

全角半角转换
半角
半角
点击月牙切换到全角
点击月牙
全角
全角

结论:
一个汉字宽度 = 一个全角空格宽度 = 4个半角空格宽度
1个半角空格 = & #160;(请删除&与#之间空格,因为如果不加这个空格,它就是个空格就显示不出来)
1个全角空格 = & #12288; = & #8195;(请删除&与#之间空格,因为如果不加这个空格,它就是个空格就显示不出来)

如果要使用strings.xml中的资源文件,会出现多个空格只显示一个的情况,解决方案:

  • 使用 多个全角空格代替
  • 使用全角或者半角空格对应的字符来替代
  • 直接在布局文件中设置文本

猜你喜欢

转载自blog.csdn.net/nongminkouhao/article/details/81512974
今日推荐