介面設計
Package Explorer裡, 在Android專案的res資料夾, right click->New->Android XML file. 選一種Layout (最基本的是LinearLayout), 點Next
點Next之後, 會出現一個設定Qualifers的介面, 這裡要小心, 如果你不知道甚麼是Qualifers, 就先不要設定, 不然啟動Activity時會有ResourcesNotFound Exception. 那是因為你有加Qualifers限制, 而AndroidManifest.xml的設定沒匹配, 就會找不到Resources, 也就是你的layout.xml會找不到.
Android介面物件的大小, 單位是dp, 例如一個ImageView的Height = 100dp. 錯了單位compiler不會過. Padding和Margin的單位是dip, 例如marginBottom=”10dip”.
要對齊元件, 要使用Container(或Layout)的Gravity屬性
如果發現Graphical顯示的結果, 長寬怪怪的, 或者orientation不對, 請在畫面設計工具上方設定target device, orientation和主題
啟動Main Activity
跟整個程式有關的設定, 都在AndroidManifest.xml.
先設定本程式會用到的資源uses-permission, 例如android.permission.INTERNET就是需要上網
AndroidManifest.xml裡應該有註明本程式所有的Activity, 一個程式裡的每一個頁面都是獨立的Activity. 要指定一個Activity作為初始, 在<activity/>裡加入<intent-filter/>, filter裡有放兩個elements, 分別是<action android:name=”android.intent.action.MAIN” />和<category android:name=”android.intent.categroy.LAUNCHER”/>
資源管理
layout的xml, drawable如圖片檔, values的xml, 都是放在res裡的資源.
加入圖片檔, 使用一般的複製貼上就可以, 但android對不同resolution的畫面可以使用不同的圖, 分別存放在後綴hdpi, ldpi, mdpi, xhdpi.
顏色定義, 命名好像規定要用Color當後綴, value可以是#RRGGBB或#AARRGGBB
利用資源id除錯. 寫以下try, catch
try
{ //do things with resources }
catch(Exception e)
{
Log.e(“ERROR”, e.toString());
}
Log會告訴你出錯的resource ID, 進去gen>package.yours>R.java 裡面有寫
內部Intent
利用內部Intent在程式內切換頁面
主動的Activity
Intent turnPageIntent = new Intent(view.getContext(), NextActivity.class);
startActivityForResult(turnPageIntent, 0);
這樣就會叫起下一個Activity
被呼叫的Activity
Intent returnIntent = new Intent();
setResult(RESULT_OK, returnIntent);
finish();
這樣就會回到前一個Activity
一定要用到的Event
在Activity裡
@Override
Public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
}
按鍵事件
Button theButton = (Button)findViewById(R.id.buttonId);
theButton.setOnClickListener(//your code);