【預習題】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裏面可以具體定義每一個元素