android - 我们应该在 Strings.xml 中还是在布局 xml 文件本身中给出字符串值

标签 android android-layout

我有一个问题,是将我们将在布局文件中使用的所有字符串文本都放在 strings.xml 文件中,还是应该在布局文件本身的“字符串文本”中给出属性值,例如android:text="some text".

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:id="@+id/ll1"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

    <Button android:text="Button" 
    android:id="@+id/butShow" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    </Button>


</LinearLayout>

那么可以使用 android:text="Button" 还是我们应该使用 android:text="@string/btnlabel"

字符串.xml

<resources>
    <string name="btnLabel">Button</string>
</resources>

我们应该在 strings.xml 中指定所有的字符串文本,或者只指定特定的。

最佳答案

check my answer hereOk

查看android.content.res.resources的源代码后和其他一些类,很明显,使用Resources和通过getResources()获取资源的成本有点高

但是使用 getResources() 有其优势:

  • 它可以帮助您外部化您的资源

For any type of resource, you can specify default and multiple alternative resources depending maybe on Locale, Screen Depth/Resolution...


关于android - 我们应该在 Strings.xml 中还是在布局 xml 文件本身中给出字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6957608/

相关文章:

java - 将链接更改为可在 TextView 上单击的自定义字符串

android - react native : Null extracted folder for artifact: ResolvedArtifact(componentIdentifier=com. facebook.react :react-native:0. 71.0-rc.0)

java - 软输入布局没有按预期改变

android - 如何根据另一个人的宽度为TextView的第一行设置缩进

android - 使用水平和垂直平移/拖动和捏缩放查看

Android layout_weight 没有按预期工作

java - 如果单击或未单击按钮,则制作按钮方法

java - Android 帐户同步提供程序

android - Android 中填充或边距的最佳做法是什么?

Android Asus 七寸 hdpi 平板布局问题