一、任务:手机信息页面
二、实验目的:
1、掌握网格布局、相对布局的使用
2、掌握样式的使用
3、掌握如何对程序进行国际化
三、实验仪器:
Android Studio
四、运行效果图:
五、 设计思路(实现原理)
1)将准备好的八个图标复制到res/drawable文件夹下
2)创建一个两列的网格布局,并在网格布局中创建8个相对布局
3)在相对布局中添加相应的TextView、ImageView
4)在values文件下的style.xml文件中存放抽取出来的样式
5)创建values-zh-rCN、values-en-rUS文件夹,并在文件夹中创建strings.xml
六、案例实现
(1)、创建“手机信息页面”程序
创建一个名为“手机信息页面”的程序,该程序用于展示手机设置页面的信息。程序界面对应布局文件activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
<!--网格列数为2-->
android:columnCount="2"
<!--设置网格布局与边框的距离-->
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.edu.bzu.a10lab3.MainActivity">
<
RelativeLayout
<!--引入定义好的样式-->
style="@style/my1_style"
<!--设置行和列的权重为1,以达到每个相对布局都平均分布-->
android:layout_columnWeight="1"
android:layout_marginRight="15dp"
android:layout_rowWeight="1">
<
ImageView
android:id="@+id/clound"
style="@style/my_style"
android:background="@drawable/clound" />
<
TextView
style="@style/my_style"
android:layout_below="@id/clound"
android:layout_marginTop="10dp"
android:text="@string/_cloud" />
</
RelativeLayout>
<
RelativeLayout
style="@style/my1_style"
android:layout_columnWeight="1"
android:layout_rowWeight="1">
<
ImageView
android:id="@+id/bluetooth"
style="@style/my_style"
android:background="@drawable/bluetooth" />
<
TextView
style="@style/my_style"
android:layout_below="@id/bluetooth"
android:layout_marginTop="10dp"
android:text="@string/_bluetooth" />
</
RelativeLayout>
<
RelativeLayout
style="@style/my1_style"
android:layout_columnWeight="1"
android:layout_marginRight="15dp"
android:layout_rowWeight="1">
<
ImageView
android:id="@+id/gestur"
style="@style/my_style"
android:background="@drawable/gesture" />
<
TextView
style="@style/my_style"
android:layout_below="@id/gestur"
android:layout_marginTop="10dp"
android:text="@string/_gestur" />
</
RelativeLayout>
<
RelativeLayout
style="@style/my1_style"
android:layout_columnWeight="1"
android:layout_rowWeight="1">
<
ImageView
android:id="@+id/gps"
style="@style/my_style"
android:background="@drawable/gps" />
<
TextView
style="@style/my_style"
android:layout_below="@id/gps"
android:layout_marginTop="10dp"
android:text="@string/_gps" />
</
RelativeLayout>
<
RelativeLayout
style="@style/my1_style"
android:layout_columnWeight="1"
android:layout_marginRight="15dp"
android:layout_rowWeight="1">
<
ImageView
android:id="@+id/info"
style="@style/my_style"
android:background="@drawable/info" />
<
TextView
style="@style/my_style"
android:layout_below="@id/info"
android:layout_marginTop="10dp"
android:text="@string/_systemInfo" />
</
RelativeLayout>
<
RelativeLayout
style="@style/my1_style"
android:layout_columnWeight="1"
android:layout_rowWeight="1">
<
ImageView
android:id="@+id/internet"
style="@style/my_style"
android:background="@drawable/internet" />
<
TextView
style="@style/my_style"
android:layout_below="@id/internet"
android:layout_marginTop="10dp"
android:text="@string/_internet" />
</
RelativeLayout>
<
RelativeLayout
style="@style/my1_style"
android:layout_columnWeight="1"
android:layout_marginRight="15dp"
android:layout_rowWeight="1">
<
ImageView
android:id="@+id/language"
style="@style/my_style"
android:background="@drawable/language" />
<
TextView
style="@style/my_style"
android:layout_below="@id/language"
android:layout_marginTop="10dp"
android:text="@string/_language" />
</
RelativeLayout>
<
RelativeLayout
style="@style/my1_style"
android:layout_columnWeight="1"
android:layout_rowWeight="1">
<
ImageView
android:id="@+id/notifycation"
style="@style/my_style"
android:background="@drawable/notifycation" />
<
TextView
style="@style/my_style"
android:layout_below="@id/notifycation"
android:layout_marginTop="10dp"
android:text="@string/_notifycation" />
</
RelativeLayout>
</
GridLayout>
(2)抽取样式
将相同代码抽取为样式放在一个style.xml文件中,style.xml文件如下所示:
<
resources>
<!-- Base application theme. -->
<
style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<
item name="colorPrimary">@color/colorPrimary</
item>
<
item name="colorPrimaryDark">@color/colorPrimaryDark</
item>
<
item name="colorAccent">@color/colorAccent</
item>
</
style>
<
style name="my_style">
<
item name="android:layout_width">wrap_content</
item>
<
item name="android:layout_height">wrap_content</
item>
<
item name="android:layout_centerHorizontal">true</
item>
</
style>
<
style name="my1_style">
<
item name="android:layout_height">wrap_content</
item>
<
item name="android:background">@android:color/white</
item>
<
item name="android:layout_width">0dp</
item>
<
item name="android:layout_marginBottom">15dp</
item>
</
style>
</
resources>
(3)抽取文字
将布局中文字抽取到一个string.xml文件中,若布局中相同的文字比较多,可以选择该方法
<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>
<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>
(4)创建values-zh-rCN、values-en-rUS文件夹
在res目录下创建values-zh-rCN、values-en-rUS文件夹,并在这两个文件夹下创建相应的string.xml文件
values-zh-rCN文件夹下的string.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文件夹下的string.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>
(5)编写与界面交互的代码
在MainActivity中编写与用户交互的逻辑代码,MainActivity对应的代码如下所示:
public class MainActivity
extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main);
}
}
(6)运行效果图
转载请注明原文地址: https://ju.6miu.com/read-35675.html