Android中样式和国际化

来源:互联网 发布:ubuntu tensorflow安装 编辑:程序博客网 时间:2024/06/05 00:57

样式:

若是在布局设置中有相同的属性,常常在 res/values/styles.xml 中配置样式,使得在修改样式时,改最少的代码。

    <style name="MyTextStyle" ><item name="android:layout_width">wrap_content</item><item name="android:layout_height">wrap_content</item><item name="android:textColor">#ffff00</item><item name="android:textSize">22sp</item><item name="android:background">#66000000</item>    </style>

使用时,在控件中加上  style="@style/MyTextStyle"  即可。

在这个文件里,还可以修改整个应用主题,只不过是换了个称谓,作用范围大些(作用在整个应用的),和样式的实质是一样的。


国际化:

这个也比较简单,在res目录下,新建一个目录比如:values-zh,表示中文汉化版,然后拷入strings.xml。

当你安卓手机设置改为中文版,则页面中用@string调用的量就会优先调用values-zh 中strings.xml中的配置。

    <string name="app_name">样式和主题</string>    <string name="action_settings">设置</string>    <string name="hello_world">你好!</string>
当在构件中使用 @string/hello_world 时,则为"你好!" 

还有最好这样写    Toast.makeText(context , R.string.XX,..).show();  也会更加有利于国际化,而不是把内容写死。
还有界面相关的内容,若是为了国际化,也应当写 @string 

总之,写代码时,能使用 R.string 或 @string 都尽量这样用。

















0 0