(转载请注明出处:http://blog.csdn.net/buptgshengod)
1.介绍
在制作android应用的时候,常常用到很多string,就是一些文字,这里介绍一种方法,将所有string封装在一个string.xml文件中,通过styles.xml控制string的大小等属性。在代码中,只要调用string的id即可。这样符合面向对象的编程思想,而且也能使android应用更好的适应各种分辨率的手机!
2.具体实现
首先,我们在value文件夹下建立string.xml用于存放string,建立styles.xml用于控制string的属性。(无视其他xml)
在string.xml中,我们声明一些string,注意name
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, GameActivity!</string>
<string name="app_name">镖镖必达</string>
<string name="help">帮助信息\n此游戏是一款单指飞镖游戏\n点击屏幕发射飞镖\n连击可以获得更高分数</string>
<string name="about">关于游戏\n\n蓟门边IT创意工作室出品\nRhythmMaker原班人马打造\n单指RPG手游巅峰之作\n技术:zixiyaoren@gmail.com
\n美术:Rou7in@gmail.com</string>
<string name="title_activity_other">OtherActivity</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
在styles.xml中
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="label_white">
<item name="android:textSize">38px</item>//字体大小
<item name="android:textColor">#0000FF</item>//颜色是蓝色
<item name="android:gravity">center</item>//居中
<item name="android:textStyle">bold</item>//加粗
</style>
</resources>
配置完xml文件,我们看看在代码中怎么调用!
比如我们要调用string.xml中命名为help的string。只要在对象中类似于这种
setText(R.string.
help
);调用方法就可以,跟图片的用法是一样的!