strings.xml 中的字串格式

在 strings.xml 檔案中定義字串也允許字串格式化。唯一需要注意的是,String 需要在下面的程式碼中處理,而不是簡單地將它附加到佈局。

<string name="welcome_trainer">Hello Pokémon Trainer, %1$s! You have caught %2$d Pokémon.</string>
String welcomePokemonTrainerText = getString(R.string.welcome_trainer, tranerName, pokemonCount);

在上面的例子中,
%1 $ s
‘%‘與普通字元分開,
‘1’表示第一個引數,
‘$‘用作引數編號和型別之間的分隔符,
’s’表示字串型別(’d’用於整數)

請注意,getString()ContextResources 的一種方法,即你可以直接在 Activity 例項中使用它,否則你可以分別使用 getActivity().getString()getContext().getString()