<xliff:g>標籤介紹:
屬性id可以隨便命名
屬性值舉例說明
%n$ms:代表輸出的是字符串,n代表是第幾個參數,設置m的值可以在輸出之前放置空格
%n$md:代表輸出的是整數,n代表是第幾個參數,設置m的值可以在輸出之前放置空格,也可以設爲0m,在輸出之前放置m個0
%n$mf:代表輸出的是浮點數,n代表是第幾個參數,設置m的值可以控制小數位數,如m=2.2時,輸出格式爲00.00
也可簡單寫成:
%d (表示整數)
%f (表示浮點數)
%s (表示字符串)
使用步驟舉例:
1.記得要在字符文件中加上這些:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
2.
<string name="test_xliff">小姐今年<xliff:g id="xxx">%1d</xliff:g>歲了,上<xliff:g id="yyy">%2s</xliff:g>年級!</string>
或者
<string name="test_xliff">小姐今年<xliff:g id="xxx">%d</xliff:g>歲了,上<xliff:g id="yyy">%s</xliff:g>年級!</string>
加上參數和空格的寫法是:
<string name="test_xliff">小姐今年<xliff:g id="xxx">%1$3d</xliff:g>歲了,上<xliff:g id="yyy">%2$5s</xliff:g>年級!</string>
3.
String test = String.format(getResources().getString(R.string.test_xliff), 7, "二");
輸出:
小姐今年7歲了,上二年級!
加上參數和空格的輸出:
小姐今年 7歲了,上 二年級!
- view sourceprint?1 String sAgeFormatString sAgeFormat1= getResources().getString(R.string.alert);
- String sFinal1 = String.format(sAgeFormat1, "李四","首都北京");
- <span style="font-size:14px;">view sourceprint?1 String sAgeFormatString sAgeFormat1= getResources().getString(R.string.alert);
- String sFinal1 = String.format(sAgeFormat1, "李四","首都北京");
- </span>