1. 国际化
布局 main.xml
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/exit"/>
|
字符串国际化
values-zh
<string name="hello">你好!</string> <string name="app_name">国际化</string> |
values-en
<string name="hello">Hello World, I18nActivity!</string> <string name="app_name">I18n</string> |
values-en-rUS
<string name="hello">Hello World, I18nActivity!,US</string> <string name="app_name">I18n</string> |
图片国际化
2. 屏幕适配
Layout-320x240
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="320x240" /> |
Layout-480x320
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="480x320" /> |
3. 样式
(详细参看文档Dev Guide)
android中的样式和CSS样式作用相似,都是用于为界面元素定义显示风格,它是一个包含一个或者多个view控件属性的集合。如:需要定义字体的颜色和大小。
在CSS中是这样定义的:
<style> .itcast{ COLOR:#0000CC; font-size:18px; } </style> |
可以像这样使用上面的css样式:
<div class="itcast">样式1</div> |
在Android中可以这样定义样式:
在res/values/styles.xml文件中添加以下内容
<?xml version="1.0" encoding="utf-8"?> <resources> <style name=“itcast”> <!-- 为样式定义一个全局唯一的名字--> <!-- name属性为样式要用在的View控件持有的属性 --> <item name="android:textSize">18px</item> <item name="android:textColor">#0000CC</item> </style> </resources> |
在layout文件中可以像下面这样使用上面的android样式:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ....> <TextView style="@style/itcast" ..... /> </LinearLayout> |
实验
values/styles.xml
<?xml version="1.0" encoding="utf-8"?> <resources>
<style name="mystyle"> <item name="android:layout_width">fill_parent</item> <item name="android:textSize">20dip</item> </style>
<style name="submystyle" parent="mystyle"> <item name="android:layout_width">wrap_content</item> <item name="android:textColor">#FF0000</item> <item name="android:gravity">center</item> <item name="android:padding">10dip</item> </style>
</resources> |
main.xml
<TextView style="@style/mystyle" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView style="@style/submystyle" android:layout_height="wrap_content" android:text="HVGA:480x320" /> |
4. 主题
android中主题也是用于为应用定义显示风格,它的定义和样式的定义相同,如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name=“itcastTheme"> <item name=“android:windowNoTitle”>true</item> <!– 没标题 à <item name=“android:windowFullscreen”>?android:windowNoTitle</item> <!– 全屏显示 --> </style> </resources> |
上面“?android:windowNoTitle”中的问号用于引用在当前主题中定义过的资源的值。下面代码显示在AndroidManifest.xml中如何为应用设置上面定义的主题:
<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/itcastTheme"> ...... </application> |
除了可以在AndroidManifest.xml中设置主题,同样也可以在代码中设置主题,如下:
setTheme(R.style.itcastTheme); |
尽管在定义上,样式和主题基本相同,但是它们使用的地方不同。
1. 样式用在单独的View,如:EditText、TextView等;主题通过AndroidManifest.xml中的<application>和<activity>用在整个应用或者某个 Activity,主题对整个应用或某个Activity进行全局性影响。
2. 如果一个应用使用了主题,同时应用下的view也使用了样式,那么当主题与样式属性发生冲突时,样式的优先级高于主题。
另外android系统也定义了一些主题,例如:<activity android:theme=“@android:style/Theme.Dialog”>,该主题可以让Activity看起来像一个对话框,如果需要查阅这些主题,可以在文档的referenceàandroid-->R.style 中查看。
实验:
values/styles.xml
<style name="mytheme"> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">?android:windowNoTitle</item> </style> |
清单文件androidmanifest.xml
<activity android:label="@string/app_name" android:name=".I18nActivity" android:theme="@style/mytheme"> …… |