开发者

Whether we should give string values in Strings.xml or in layput xml file itself

开发者 https://www.devze.com 2023-03-26 08:10 出处:网络
I have one ques that whether it is开发者_Python百科 good to give all string text that we are going to use in our layout file in strings.xml file or should we give it in \"string text\" in layout file

I have one ques that whether it is开发者_Python百科 good to give all string text that we are going to use in our layout file in strings.xml file or should we give it in "string text" in layout file itself for attribute values like 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>

then is it fine to give android:text="Button" or we should use android:text="@string/btnlabel"

strings.xml

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

We should specify all string text in strings.xml or only specific ones.


check my answer hereOk

After looking into the source code of android.content.res.resources and some other classes, it is evident that using Resources and getting resources through getResources() is a bit costly.

However, using getResources() has its advantages:

  • It helps you externalize your resources.

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



You will do better to extract all your strings to strings.xml incase you ever decide to internationalize your app! then you only have to do a different strings.xml for each language rather than layouts too

0

精彩评论

暂无评论...
验证码 换一张
取 消