【问题标题】:TextView textSize and @string in AndroidAndroid中的TextView textSize和@string
【发布时间】:2013-12-15 09:28:39
【问题描述】:

我有以下 xml 代码:

<TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Press Button"                  <!--Warning -->
          android:textSize="45dp"                      <!--Warning -->
          android:layout_gravity="center"
          android:gravity="center"
          android:id="@+id/tvDisplay" />

在 xml 代码中,我首先发现了两个警告,dp 包含我确实需要使用 sp 的警告。为什么会这样显示?

第二个警告并且可能是错误是我正在使用android:text="Press Button" 它告诉我确实使用@string。如果我使用相同的@string,则会以看起来很尴尬的文本显示。这是什么原因!

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    View 中的硬编码String 值不被developer.android.com 推荐,因为制作与不同语言兼容的Android 应用程序被扭曲了。

    引用自

    要添加对更多语言的支持,请在 res/ 内创建附加值目录,其中包括连字符和目录名称末尾的 ISO 国家/地区代码。例如,values-es/ 是包含语言代码“es”的语言环境的简单资源的目录。 Android 在运行时根据设备的区域设置加载适当的资源。

    一旦您决定了您将支持的语言,请创建资源子目录和字符串资源文件。例如:

    MyProject/
    res/
       values/
           strings.xml
       values-es/
           strings.xml
       values-fr/
           strings.xml
    

    将每个语言环境的字符串值添加到相应的文件中。

    在运行时,Android 系统会根据当前为用户设备设置的区域设置使用适当的字符串资源集。

    例如下面是针对不同语言的一些不同的字符串资源文件。

    英语(默认语言环境),/values/strings.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string name="title">My Application</string>
    <string name="hello_world">Hello World!</string>
    </resources>
    

    西班牙语,/values-es/strings.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string name="title">Mi Aplicación</string>
    <string name="hello_world">Hola Mundo!</string>
    </resources>
    

    参考您的 OP:

    XML 文件保存在 res/values/strings.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string name="press">Press Button</string>
    </resources>
    

    此布局 XML 将字符串应用于视图:

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/press"      
    android:textSize="45dp"    <!--Warning -->
    android:layout_gravity="center"
    android:gravity="center"
    android:id="@+id/tvDisplay"  />
    

    此应用程序代码检索字符串:

    String string = getString(R.string.hello);
    

    按照developer.android.com 的建议,使用 sp 设置 size

    sp : Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.

    保存在 res/values/dimens.xml 的 XML 文件:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <dimen name="font_size">16sp</dimen>
    </resources>
    

    此应用程序代码检索维度

    Resources res = getResources();
    float fontSize = res.getDimension(R.dimen.font_size);
    

    此布局 XML 将尺寸应用于属性:

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Press Button"      <!--Warning -->
    android:textSize="@dimen/font_size"
    android:layout_gravity="center"
    android:gravity="center"
    android:id="@+id/tvDisplay" 
    />
    

    【讨论】:

      【解决方案2】:

      对于缩放像素,推荐的文本尺寸类型为“sp”(例如:15sp)

      来自developer.android

      【讨论】:

        【解决方案3】:

        Android 标准 TextView 大小是使用 SP 并且你是我给出警告的硬编码字符串。

        请在值文件夹中使用您的String.xml 并选择此字符串,然后它不会给出任何错误。

        谢谢

        【讨论】:

          【解决方案4】:

          最好to use SP instead of DP

          建议总是使用字符串资源文件,因为如果您需要更改多个xml文件中使用的单个@String,您只需更改一次。反之亦然,如果您将字符串写入 xml 布局文件中,如果您需要更改字符串,则需要搜索字符串,搜索 xml 文件,然后根据需要更改尽可能多的出现次数。

          总之,硬编码字符串不是一个好习惯。您应该将它们指定为字符串资源文件,然后在布局中引用它们。这允许您通过编辑 strings.xml 文件同时更新所有布局中单个单词的每次出现 .

          还需要支持多种语言定义为单独的strings.xml 一种语言的一个文件!

          【讨论】:

            猜你喜欢
            • 2013-01-24
            • 1970-01-01
            • 1970-01-01
            • 2018-06-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多