android String ,StringArrays , Plurals ,以及Formatting and Styling

 然后下面是自己的理解:

  String ,string-array, Plurals

都是可以定义在 :res/values/strings.xml:

  大家对String ,string-array 可能很明白,用的比较的多,因为直接alt+/ 快键方式就有提示,然后你就能看到 这些标签

  用起来也很简单,在/strings.xml: 定义之后,可以通过 r.strinb.xxxx 能找到你定义

 

<?xml version="1.0" encoding="utf-8"?><resources>  

 <string name="hello">Hello!</string></resources>

 做了android也快很久了,就是没有养成认真看androidSdk 文档的习惯 ,一贯都是用到哪就去查找,所以今天反编译别人的代码看到了

Plurals 这个标签,感觉自己没用过,直接去sdk 中查找,发现很使用的一个标签,其实android给了很多方便的便签和优化的解决方案,关键是你是否认真的查看的了

他官方的文档:

  废话补不多说: sdk 介绍:http://www.gfan.com/dev/android/guide/topics/resources/string-resource.html

xml中可以这样引用<TextView

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello" />  程序中代码可以这样得到String string = getString(R.string.hello); 这是 string-array

<?xml version="1.0" encoding="utf-8"?><resources>    <string-array name="planets_array">             <item>Mercury</item>             <item>Venus</item>             <item>Earth</item>             <item>Mars</item>    </string-array></resources> 程序代码:Resources res = getResources();String[] planets = res.getStringArray(R.array.planets_array); Plurals  一对字符串对应了不好几个不同的词语或者短语,在你的应用的程序中可以一个string 对用引用他,当你请求获取Plurals(复数)中一个资源的时候你可以通过getQuantityString(), 但是你指定count 这个参数,这样你就能获取你定义好的复数对应的参数。   简单的说就是用一个字符串对应多个短语,但是你可以根据角标来确定你最后到底对应把一个,跟switch 很像<?xml version="1.0" encoding="utf-8"?>

<resources>

    <plurals name="numberOfSongsAvailable">

        <item quantity="one">One song found.</item>

        <item quantity="other">%d songs found.</item>

    </plurals>

</resources>java 代码int count = getNumberOfsongsAvailable();

Resources res = getResources();

String songsFound = res.getQuantityString(R.plurals.numberOfSongsAvailable, count);

 note:注意的是 quantity  只能取one ,zero ,other  枚举是有限的 最后就是

 

Formatting and Styling

 

 <string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

 

In this example, the format string has two arguments: %1$s is a string and%2$d is a decimal number. You can format the string with arguements from your application like this:

 

Resources res = getResources();

String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

这样你应该懂了

  好了希望对你有帮助:转载请注明出入!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值