1. 掌握相对布局、线性布局的使用
2. 掌握样式的使用
3. 掌握如何对程序进行国际化
2、设计思路(实验原理)
1)将准备好的八个图标复制到res/drawable文件夹下
2)创建一个垂直的线性布局,并在线性布局中创建4个相对布局
3)在相对布局中添加相应的TextView
4)在values文件下的style.xml文件中存放抽取出来的样式
5)创建values-zh-rCN、values-en-rUS文件夹,并在文件夹中创建strings.xml文件
3、实现代码
(1)创建“手机信息页面”程序
创建一个名为“手机信息页面”的程序,该程序用于展示手机设置页面的页面。程序界面对应布局文件activity_mian.xml如下所示:
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/darker_gray" android:columnCount="2" tools:context="cn.edu.bzu.phoneinfo.MainActivity"> <LinearLayout style="@style/h_wrap_content" android:layout_marginTop="25dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="8dp" android:background="@drawable/clound" /> <TextView style="@style/tv_style" android:paddingTop="8dp" android:text="@string/_cloud" /> </LinearLayout> <LinearLayout style="@style/h_wrap_content" android:layout_column="1" android:layout_marginTop="25dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="8dp" android:background="@drawable/bluetooth" /> <TextView style="@style/tv_style" android:paddingTop="6dp" android:text="@string/_bluetooth" /> </LinearLayout> <LinearLayout
style="@style/h_wrap_content" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="8dp" android:background="@drawable/gesture" /> <TextView style="@style/tv_style" android:paddingTop="6dp" android:text="@string/_gesture" /> </LinearLayout> <LinearLayout style="@style/h_wrap_content" android:layout_column="1"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="8dp" android:background="@drawable/gps" /> <TextView style="@style/tv_style" android:paddingTop="6dp" android:text="@string/_gps" /> </LinearLayout> <LinearLayout style="@style/h_wrap_content"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="8dp" android:background="@drawable/info" /> <TextView style="@style/tv_style" android:paddingTop="6dp" android:text="@string/_system_info" /> </LinearLayout> <LinearLayout style="@style/h_wrap_content" android:layout_column="1"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="8dp" android:background="@drawable/internet" /> <TextView style="@style/tv_style" android:paddingTop="6dp" android:text="@string/_internet" /> </LinearLayout> <LinearLayout style="@style/h_wrap_content"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="8dp" android:background="@drawable/language" /> <TextView style="@style/tv_style" android:paddingTop="6dp" android:text="@string/_language" /> </LinearLayout> <LinearLayout style="@style/h_wrap_content" android:layout_column="1"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="8dp" android:background="@drawable/notifycation" /> <TextView style="@style/tv_style" android:paddingTop="6dp" android:text="@string/_set_notifycation" /> </LinearLayout> </GridLayout>
(2)抽取样式
由于编写布局文件时,相同控件之间的外边距和宽高都是固定的。因此会产生大量重复的布局代码,为了代码简洁和重复使用可以将相同代码抽取为样式单独放在一个style.xml文件中。style.xml文件如下所示:
<resources> <style name="AppBaseTheme" parent="android:Theme.Light"> </style> <style name="AppTheme" parent="AppBaseTheme"> </style> <!-- 每个线性布局的宽度和高度高 --> <style name="h_wrap_content"> <item name="android:layout_width">150dp</item> <item name="android:layout_height">100dp</item> <item name="android:background">@android:color/white</item> <item name="android:layout_marginBottom">25dp</item> <item name="android:layout_marginLeft">20dp</item> <item name="android:orientation">vertical</item> </style> <!-- 宽 match——parent --高wrap——content--> <style name="tv_style"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:gravity">center</item> </style> </resources>(3)创建values-zh-rCN、values-en-rUS文件
在res目录下创建values-zh-rCN、values-en-rUS文件夹,并在这两个文件夹下创建相应的strings.xml文件。
values-zh-rCN文件夹下的strings.xml文件如下所示:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">手机信息页面</string> <string name="menu_settings">设置</string> <string name="hello_world">你好,世界!</string> <string name="_cloud">云通信</string> <string name="_bluetooth">蓝牙</string> <string name="_gesture">自定义手势</string> <string name="_gps">定位</string> <string name="_system_info">系统信息</string> <string name="_internet">网络</string> <string name="_language">语言设置</string> <string name="_set_notifycation">通知栏设置</string> </resources>values-en-rUS文件夹下的strings.xml文件如下所示:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">phoneInfo</string> <string name="menu_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="_cloud">Cloud</string> <string name="_bluetooth">Bluetooth</string> <string name="_gesture">Gesture</string> <string name="_gps">Gps</string> <string name="_system_info">SystemInfo</string> <string name="_internet">Internet</string> <string name="_language">Language</string> <string name="_set_notifycation">Notifycation</string> </resources>(4)编写与界面交互的代码
接下来需要在MainActivity中编写与用户交互的逻辑代码,MainActivity对应的代码如下所示:
public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }4、 运行效果图