问题
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