我有一个问题,是将我们将在布局文件中使用的所有字符串文本都放在 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 中指定所有的字符串文本,或者只指定特定的。
最佳答案
查看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/