【预习题】pre1. 安装并配置好Android开发环境,然后运行一个测试Android程序。请将测试结果截图粘贴至下方(同时,要求在该截图中看到计算机的基本信息,如硬件信息、计算机名、产品ID等)。
----------------------------------------
1.使用RelativityLayout布局,设计如下图所示的程序界面。
<?xml version="1.0" encoding="utf-8"?>
<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: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="com.tao.huangkangquan.huangkangquan">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON1"
android:id="@+id/button"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON2"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:layout_toLeftOf="@+id/button"
android:layout_toStartOf="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON3"
android:id="@+id/button3"
android:layout_alignTop="@+id/button2"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON4"
android:id="@+id/button4"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true" />
</RelativeLayout>
2.使用LinearLayout布局,设计如下图所示的程序界面。
<?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"> <EditText android:id="@+id/et_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="right"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" android:layout_marginRight="5dp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清除" android:layout_marginRight="10dp"/> </LinearLayout> </LinearLayout>
3. 使用TableLayout布局,设计如下图所示的程序界面。
<?xml version="1.0" encoding="utf-8"?> <TableLayout 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"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="处理器" android:layout_marginTop="10dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hisiios" android:layout_alignParentRight="true" android:layout_marginTop="10dp" />/> </RelativeLayout> <View android:layout_height="1px" android:background="#000000" android:layout_marginTop="10dp" android:layout_width="fill_parent"></View> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="运行内存" android:layout_marginTop="10dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3.GB" android:layout_alignParentRight="true" android:layout_marginTop="10dp" />/> </RelativeLayout> <View android:layout_height="1px" android:background="#000000" android:layout_marginTop="10dp" android:layout_width="fill_parent"></View> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="手机存储" android:layout_marginTop="10dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="16.0GB" android:layout_alignParentRight="true" android:layout_marginTop="10dp" />/> </RelativeLayout> <View android:layout_height="1px" android:background="#000000" android:layout_marginTop="10dp" android:layout_width="fill_parent"></View> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="分辨率" android:layout_marginTop="10dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1080x1920" android:layout_alignParentRight="true" android:layout_marginTop="10dp" />/> </RelativeLayout> <View android:layout_height="1px" android:background="#000000" android:layout_marginTop="10dp" android:layout_width="fill_parent"></View> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Android版本" android:layout_marginTop="10dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5.0.2" android:layout_alignParentRight="true" android:layout_marginTop="10dp" />/> </RelativeLayout> <View android:layout_height="1px" android:background="#000000" android:layout_marginTop="10dp" android:layout_width="fill_parent"></View> </TableLayout>
4. 在完成的第2题的基础上进行事件处理编程。当单击“确定”按钮时,通过Toast显示文本框中输入的内容;当单击“清除”按钮时,清空文本框内容。
package com.tao.huangkangquan; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class huangkangquan extends AppCompatActivity { private EditText et_input; //文本接受框 private Button but_yes; //确定按钮 private Button but_no; //取消按钮 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_huangkangquan); //得到用户输入的信息 et_input = (EditText) findViewById(R.id.et_input); but_yes = (Button) findViewById(R.id.but_yes); but_no = (Button) findViewById(R.id.but_no); //当用户点下确定按钮时显示的信息 but_yes.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ Toast.makeText(huangkangquan.this, et_input.getText(), Toast.LENGTH_SHORT).show(); } }); //当用户点取消按钮清空输入框 but_no.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ et_input.setText(""); } }); } }
【扩展题】5. 实现如下图所示的程序界面。要求:组件上的文字内容在values文件夹下的strings.xml文件中定义;一些自定义的样式放在values文件夹下的style.xml文件中定义。
文件名称:activity_huangkangquan.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:background="@android:color/darker_gray" android:orientation="vertical" tools:context=".MainActivity"> <RelativeLayout style="@style/h_wrap_content" android:layout_marginTop="10dp"> <TextView style="@style/tv_style" android:layout_alignParentLeft="true" android:layout_marginLeft="10dp" 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:text="@string/_bluetooth"/> </RelativeLayout> <RelativeLayout style="@style/h_wrap_content" android:layout_marginTop="10dp"> <TextView style="@style/tv_style" android:layout_alignParentLeft="true" android:layout_marginLeft="10dp" android:drawableTop="@drawable/gesture" android:text="@string/_gesture" /> <TextView style="@style/tv_style" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:drawableTop="@drawable/gps" android:text="@string/_gps" /> </RelativeLayout> <RelativeLayout style="@style/h_wrap_content" android:layout_marginTop="10dp"> <TextView style="@style/tv_style" android:layout_alignParentLeft="true" android:layout_marginLeft="10dp" android:drawableTop="@drawable/info" android:text="@string/_system_info" /> <TextView style="@style/tv_style" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:drawableTop="@drawable/internet" android:text="@string/_internet" /> </RelativeLayout> <RelativeLayout style="@style/h_wrap_content" android:layout_marginTop="10dp"> <TextView style="@style/tv_style" android:layout_alignParentLeft="true" android:layout_marginLeft="10dp" android:drawableTop="@drawable/language" android:text="@string/_language" /> <TextView style="@style/tv_style" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:drawableTop="@drawable/notifycation" android:text="@string/_set_notifycation" /> </RelativeLayout> </LinearLayout>
文件名称:strings.xml
<?xmlversion="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>
文件名称:styles.xml
<resources> <style name="AppBaseTheme" parent="android:Theme.Light"> </style> <style name="AppTheme" parent="AppBaseTheme"> </style> <!-- 宽 match——parent 高 wrap_content--> <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">175dp</item> <item name="android:layout_height">90dp</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>
- 实验思考
- Android体系结构分为哪些层次,其功能分别是什么?
应用程序层:在这个层面开发的应用程序都是Java语言编写,调用 Application Framwork 提供的API来完成。
系统运行库层:主要通过JNI(Java Nation Interface)的方式向应用程序框架层提供调用底层程序库(Libraries)的接口
- SDK有哪些主要文件夹,其内容分别是什么?
1.add-ons 这里面保存着附加库,比如google Maps,当然你如果安装了OPhone SDK,这里也会有一些类库在里面。
2.docs 这里面是Android SDK API参考文档,所有的API都可以在这里查到。
market_licensing 作为Android Market版权保护组件,一般发布付费应用到电子市场可以用它来反盗版。
3.platforms 是每个平台的SDK真正的文件,里面会根据API Level划分的SDK版本, 这里就以Android 2.2来说,进入后有一个android-8的文件夹,android-8进入后是Android 2.2 SDK的主要文件,其中ant为ant编译脚本,data保存着一些系统资源,images是模拟器映像文件,skins则是Android模拟器的皮肤,templates是工程创建的默认模板,android.jar则是该版本的主要framework文件,4.tools目录里面包含了重要的编译工具,比如aapt、aidl、逆向调试工具dexdump和编译脚本dx。
5.platform-tools 保存着一些通用工具,比如adb、和aapt、aidl、dx等文件,Android123提示,这里和platforms目录中tools文件夹有些重复,主要是从android 2.3开始这些工具被划分为通用了。
6.samples 是Android SDK自带的默认示例工程,里面的apidemos强烈推荐初学者运行学习,对于SQLite数据库操作可以查看NotePad这个例子,对于游戏开发 Snake、LunarLander都是不错的例子,对于Android主题开发Home则是android m5时代的主题设计原理。
7.tools 作为SDK根目录下的tools文件夹,这里包含了重要的工具,比如ddms用于启动Android调试工具,比如logcat、屏幕截图和文件管理器,而draw9patch则是绘制android平台的可缩放png图片的工具,sqlite3可以在PC上操作SQLite数据库,而 monkeyrunner则是一个不错的压力测试应用,模拟用户随机按键,mksdcard则是模拟器SD映像的创建工具,emulator是android模拟器主程序,不过从android 1.5开始,需要输入合适的参数才能启动模拟器,traceview作为android平台上重要的调试工具。
8.usb_driver 顾名思义,保存着android平台google官方机型的驱动如nexus one、nexus s,同时也有一些老机型驱动的支持,比如说htc dream、htc magic和Motorola 的droid。
3. Android提供哪些主要布局方式,其特点分别是什么?
1.FrameLayout,特点:所有的View都会放在左上角,并且后添加进去的View会覆盖之前放进去的View。
2.LinearLayout,特点:在水平或者垂直方向上依次按照顺序来排列子元素,控件的排列顺序遵循其在布局文件中被写出的先后顺序。
3.RelativeLayout,特点:以某一个子元素为参照物,其余子元素均按照其相对位置来完成布局。
4.TableLayout,特点:每个TableLayout都是由一个或多个TableRow组成的,一个TableRow就代表TableLayout的一行。
5.AbsoluteLayout,表格布局类似Html里面的Table。每一个TableLayout里面有表格行TableRow,TableRow里面可以具体定义每一个元素