6.拿hello world举个例子
1.在程序中获取string.xml中字符串和数值
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MainActivity!</string>
<string name="app_name">TestExample01</string>
</resources>
在Activity中使用:
String appName=(String) this.getResources().getText(R.string.app_name);
Log.i("test", "appName="+appName);
或者:
String appName=(String) this.getResources().getString(R.string.app_name);
Log.i("test", "appName="+appName);
2.定义string数组(arrays.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="sports">
<item>足球</item>
<item>篮球</item>
<item>太极</item>
<item>冰球</item>
</string-array>
</resources>
----getResources().getStringArray(R.string.sports);
3.定义颜色(colors.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#FFFFFF</color>
</resources>
---getResources().getDrawable(R.string.black);
---getResources().getColor(R.string.black);
4.定义尺寸(dimens.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="height">80dip</dimen>
</resources>
---getResource().getDimension(R.string.height);
5.定义样式(styles.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="sharpText">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#000000</item>
</style>
</resources>
这是strings.xml文件