android中string.xml中%1$s、%1$d等的用法

來源:互聯網
上載者:User

標籤:

今天在研究前輩寫的代碼的時候,突然發現string裡面出現了<stringname="item_recent_photo">最近拍攝%1$s</string>
有點蒙圈,這個不是c語音裡面的表示方式,就百度了一下,記。。。

 

在TextView中想要動態顯示某些值,用到%1$s,%1$d,先介紹一下:

  • %n$ms:代表輸出的是字串,n代表是第幾個參數,設定m的值可以在輸出之前放置空格 
  • %n$md:代表輸出的是整數,n代表是第幾個參數,設定m的值可以在輸出之前放置空格
  • %n$mf:代表輸出的是浮點數,n代表是第幾個參數,設定m的值可以控制小數位元,如m=2.2時,輸出格式為00.00
  • %d   (表示整數)
  • %f    (表示浮點數)
  • %s   (表示字串)
使用:1、整型,比如“小姐今年23歲了”,這個23是整型的。在string.xml中可以這樣寫,<string name="old">小姐今年%1$d歲了</string>

String sAgeFormat = getResources().getString(R.string.old);  

String sFinalAge = String.format(sAgeFormat, 23); 

將%1$d替換為23; %1$d表達的意思是整個name=”old”中,第一個整型的替代。如果一個name中有兩個需要替換的整型內容,則第二個寫為:%2$d,以此類推 2、string型,比如“我的名字叫李四,我來自首都北京”;這裡的“李四”和“首都北京”都需要替換。 在string.xml中可以這樣寫,<string name="alert">我的名字叫%1$s,我來自%2$s</string>

view sourceprint?1 String sAgeFormatString sAgeFormat1= getResources().getString(R.string.alert);      

String sFinal1 = String.format(sAgeFormat1, "李四","首都北京");

android中string.xml中%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.