Android 实验3 手机信息界面的编写
一、问题引出
编写一个手机信息界面的UI编写,并且支持中英文语言的切换。
程序最终运行结果如图所示:
英文界面
中文界面
二、设计思路
1)将准备好的八个图标复制到res/drawable文件夹下
2)创建一个垂直的线性布局,并在线性布局中创建4个相对布局
3)在相对布局中添加相应的TextView
4)在values文件下的style.xml文件中存放抽取出来的样式
5)创建values-zh-rCN、values-en-rUS文件夹,并在文件夹中创建strings.xml文件
三、案例实现
在Andorid Studio 中创建PhoneInfo工程,导入图片素材到drawable文件夹中,在activity_main.xml文件中编写布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:background="@android:color/darker_gray"
tools:context="com.edu.bzu.phoneinfo.MainActivity">
<RelativeLayout
android:layout_marginTop="10dp"
style="@style/h_wrap_content">
<TextView
style="@style/tv_style"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content"
android:drawableTop="@drawable/clound"
android:text="@string/_cloud"
/>
<TextView
style="@style/tv_style"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:drawableTop="@drawable/bluetooth"
android:layout_height="wrap_content"
android:text="@string/_bluetooth"/>
</RelativeLayout>
<RelativeLayout
android:layout_marginTop="10dp"
style="@style/h_wrap_content">
<TextView
style="@style/tv_style"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:drawableTop="@drawable/gesture"
android:layout_height="wrap_content"
android:text="@string/_gesture"
/>
<TextView
style="@style/tv_style"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:drawableTop="@drawable/gps"
android:layout_height="wrap_content"
android:text="@string/_gps"/>
</RelativeLayout>
<RelativeLayout
android:layout_marginTop="10dp"
style="@style/h_wrap_content">
<TextView
style="@style/tv_style"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:drawableTop="@drawable/info"
android:layout_height="wrap_content"
android:text="@string/_system_info"
/>
<TextView
style="@style/tv_style"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:drawableTop="@drawable/internet"
android:layout_height="wrap_content"
android:text="@string/_internet"/>
</RelativeLayout>
<RelativeLayout
android:layout_marginTop="10dp"
style="@style/h_wrap_content">
<TextView
style="@style/tv_style"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:drawableTop="@drawable/language"
android:layout_height="wrap_content"
android:text="@string/_language"
/>
<TextView
style="@style/tv_style"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:drawableTop="@drawable/notifycation"
android:layout_height="wrap_content"
android:text="@string/_set_notifycation"
/>
</RelativeLayout>
</LinearLayout>
在value文件夹中的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>
<!--宽match -parent 高 wrap cpntent -->
<style name="h_wrap_content">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
<!-- 宽高都 match parent -->
<style name="tv_style">
<item name="android:layout_width">145dp</item>
<item name="android:layout_height">50dp</item>
<item name="android:gravity">center</item>
<item name="android:paddingTop">8dp</item>
<item name="android:paddingBottom">8dp</item>
<item name="android:drawablePadding">5dp</item>
<item name="android:background">@android:color/white</item>
</style>
</resources>
创建中英文语言界面
中文:在res文件夹中创建values-zh-rCN文件夹,并在其中创建strings.xml文件,在其中输入中文界面字符:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">手机信息页面</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>
英文界面 在res文件夹创建values-en-rUS文件夹,在其中创建strings.xml文件,输入以下代码
代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">PhoneInfo</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>
编写交互界面代码:
在MainActivity.java文件中保留原始代码。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
已从测试模拟机测试通过!(Android7.0 API 24)