Load from string.xml resources files depending on gender in android

与世无争的帅哥 提交于 2019-12-10 17:31:15

问题


I am working on application where themes and strings differs when user is male or female. I could control colors depending on user gender but now i can't solve problem of strings for example if user is a male, I will use

<string name="welcome">Welcome boy</string>

but if user female it will be

<string name="welcome">Welcome girl</string>

that is just for example.so how can i load the correct file from resources depending on gender value which is stored in variable ??


回答1:


There's no direct support for what you are looking for in the plaform, so you need to write the logic yourself and have two strings per entry (i.e. welcome_m for males and welcome_f for females). But you need to set it on your widgets from code depending of who is using your app.




回答2:


You have to create 2 resources id for your string

<string name="welcome_boy">Welcome boy</string>

<string name="welcome_girl">Welcome girl</string>

if (isBoy())
    txtWelcome.setText(getResources().getString(R.id.welcome_boy));
else 
    txtWelcome.setText(getResources().getString(R.id.welcome_girl));

Is that what you want?




回答3:


Define array for gender-specific expressions, for example:

<string-array name="welcome">
    <item>Welcome Boy</item>
    <item>Welcome Girl</item>
</string-array>

Define for yourself the [0] item is male and [1] is female



来源:https://stackoverflow.com/questions/31710733/load-from-string-xml-resources-files-depending-on-gender-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!