Android 預留位置 %1$s %1$d

來源:互聯網
上載者:User

標籤:android 預留位置

1、整型,比如“我今年23歲了”,這個23是整型的。在string.xml中可以這樣寫,<string name="old">我今年%1$d歲了</string> 
在程式中,使用  [java] view plaincopy
  1. String sAgeFormat = getResources().getString(R.string.old);  
  2. String sFinalAge = String.format(sAgeFormat, 23);  
將%1$d替換為23; 
%1$d表達的意思是整個name=”old”中,第一個整型的替代。如果一個name中有兩個需要替換的整型內容,則第二個寫為:%2$d,以此類推;具體程式中替換見下面的string型;  
 2、string型,比如“我的名字叫李四,我來自首都北京”;這裡的“李四”和“首都北京”都需要替換。 
在string.xml中可以這樣寫,<string name="alert">我的名字叫%1$s,我來自%2$s</string> 
在程式中:  [java] view plaincopy
  1. view sourceprint?1 String sAgeFormatString sAgeFormat1= getResources().getString(R.string.alert);     
  2.   
  3. String sFinal1 = String.format(sAgeFormat1, "李四","首都北京");   
這裡兩個string需要替換的,按照上面程式的順序依次對應。  

<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">%d</xliff:g>歲了,上<xliff:g id="yyy">%s</xliff:g>年級!</string>

3. 

String test = String.format(getResources().getString(R.string.test_xliff), 7, "小學二");

輸出:

小紅今年7歲了,上小學二年級!

Android 預留位置 %1$s %1$d

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.