【攻克Android (3)】资源国际化、屏幕适配

[b][size=large]本文围绕以下两个部分展开: [/size][/b]

[b][size=large]一、资源国际化[/size][/b]
[b][size=large]二、屏幕适配[/size][/b]


[b][size=large]一、资源国际化[/size][/b]

[size=medium][b]1.在 res -> values -> strings.xml 中,定义所有所需的文本[/b][/size]

 <resources>  
<string name="app_name">UI</string>

<string name="action_settings">Settings</string>

<string name="app">App</string>
<string name="game">Game</string>
<string name="movie">Movie</string>
<string name="music">Music</string>

</resources>


[size=medium]定义字符串常量,遵守“名值对”。[/size]
[size=medium]如: <string name="app">App</string> 中,名是app,值是 App[/size]

[size=medium][b]2.在 res -> layout -> activity_main.xml 布局文件中,设计主活动布局[/b][/size]

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<Button
android:id="@+id/btn_app"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app"/>

<Button
android:id="@+id/btn_game"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_app"
android:text="@string/game"/>

<Button
android:id="@+id/btn_movie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_game"
android:text="@string/movie"/>

<Button
android:id="@+id/btn_music"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_movie"
android:text="@string/music"/>

</RelativeLayout>



[size=medium]代码说明:[/size]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/3082/10a75f8b-7d14-3d1b-a273-99f938911746.png[/img][/align]

[size=medium][b]3.资源国际化(internationalization, i18n)[/b][/size]

[size=medium]资源国际化之前,由于是用英文开发的,因此app的名字和界面中的内容都是英文的,如下。[/size]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/3084/30dd0192-ef99-3ff0-a9fb-df4e1a801e50.png[/img][/align]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/3086/6454f1b1-ca92-3a50-ab22-68c4a1c6e69e.png[/img][/align]

[size=medium]资源国际化有以下几步:[/size]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/3088/42f19969-f2af-340e-a355-7fa796e930e7.png[/img][/align]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/3090/8e5fdb5d-4d6e-3d67-9c82-099245139c04.png[/img][/align]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/3092/08c318b0-072a-39b9-9098-1e2e543c002b.png[/img][/align]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/3094/071e5fb8-fc3d-3751-b29a-cfdc8703bdce.png[/img][/align]

[size=medium]资源国际化之后,因为本人手机语言是中文,因此app的名字和界面中的内容都已显示中文,如下。[/size]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/3096/8c9f1f65-2ad4-3c4d-b22c-910da662b283.png[/img][/align]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/3098/e8d0dd59-2e4d-3d6f-95dd-09e5dc1c2997.png[/img][/align]


[b][size=large]二、屏幕适配[/size][/b]

[size=medium]手机默认是竖屏显示,因此需要创建并编写横屏的布局文件,以便在竖屏和横屏的时候,加载不同的布局文件,来实现屏幕的适配。如下分别是竖屏和横屏的显示效果。[/size]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/3104/d294fe7e-b5c1-3a9b-8d21-c89036bd270a.png[/img][/align]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/3106/d5ba6ec7-cca7-3656-9737-e70df2bfa7a0.png[/img][/align]

[size=medium]步骤如下:[/size]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/3108/0fea931b-011a-3a7d-9360-ab98c3491b8f.png[/img][/align]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/3110/0dff73e3-fcb2-36ab-8c5d-3887334be693.png[/img][/align]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/3112/7faa2e7d-2afc-3481-8af3-5381ad507b3a.png[/img][/align]

[size=medium]产生 activity_main.xml(land) 后,再在里面写布局代码,之后部署即可看到效果。[/size]

 <?xml version="1.0" encoding="utf-8"?>  
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*">

<!-- TableLayout 表格布局
stretchColumns 任意列
TableRow 表格行
layout_column 表格行中第几列
-->

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/btn_app"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:text="@string/app"/>

<Button
android:id="@+id/btn_game"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="@string/game"/>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/btn_movie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:text="@string/movie"/>

<Button
android:id="@+id/btn_music"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="@string/music"/>
</TableRow>

</TableLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值