【智能家居】
主要任務:
1.Linux伺服器部署
2.上位機(控制端)程式開發
【內建開發環境】
Eclipse,原因是友善配置無需下載下傳sdk。
AS也可以。
Eclipse啟動程式的路徑大概是:
你的路徑\adt-bundle-windows-x86_64-20140321\adt-bundle-windows-x86_64-20140321\adt-bundle-windows-x86_64-20140321\eclipse\eclipse.exe
【通信原理】
Socket通信:
伺服器使用C語言、App使用的Java。
伺服器使用結構體通信,Java沒有結構體。
怎麼辦呢?
【項目結構】
src檔案夾:存放java代碼的檔案夾
gen檔案夾:系統自動生成的java檔案,不要修改。
bin檔案夾:程式編譯運作後,生成的apk安裝包在此檔案夾下生成。
libs檔案夾:存放jar包的檔案夾(本次實訓會引入第三方的jar包)
res檔案夾:主流資源檔案夾
drawable檔案夾:存放圖檔
layout檔案夾:布局檔案夾
AndroidManifest.xml:清單檔案,用于配置項目參數
【建立項目的要求】
1.進入到src—包名—MainActivity.java檔案中,删部分代碼:
//位于onCreate方法中
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
-
在MainActivity.java檔案中的onCreate方法裡,修改setContentView的參數為:
R.layout.activity_main → R.layout.fragment_main
3.删除layout檔案夾中的activity_main.xml
4.選中項目,滑鼠右鍵—Run As — Android Application
【自動補全快捷鍵】
Eclipse Alt + /
AS Alt + Enter
【布局】
//給控件起id
android:id="@+id/text1"
//寬度
android:layout_width=“match_parent”
//高度
android:layout_height=“match_parent”
寬度和高度的可選值:
1.match_parent 比對父級
2.wrap_content 比對子級
3.fill_parent 不用,等同match_parent
4.可以使用固定數值,機關是dp
//背景
android:background="#56BB34"
内邊距 padding
android:padding=“50dp”
另外可以單獨設定上下左右的内邊距
外邊距 margin
android:layout_margin=“40dp”
另外可以單獨設定上下左右的外邊距
線性布局(LinearLayout)
控件橫着或者豎着排成一排。
//方向
android:orientation=“horizontal”
可選值:
horizontal 水準的
vertical 垂直的
//子控件的重力方向
android:gravity=“right|center_vertical”
可選值:
left(左,預設值)、top(上,預設值)、right(右)、bottom(底)、
center_horizontal(水準中心)、center_vertical(垂直中心)、center(正中心)
相對布局(RelativeLayout)
某個控件相對某個參照物在某個位置。
參照物必須要有id
//在text1控件的右邊
android:layout_toRightOf="@id/text1"
//在text1控件的左邊
android:layout_toLeftOf="@id/text1"
//在text1控件的上面
android:layout_above="@id/text1"
//在text1控件的下面
android:layout_below="@id/text1"
//對齊text1控件的四個方向
android:layout_alignRight="@id/text1"
android:layout_alignBottom="@id/text1"
android:layout_alignLeft="@id/text1"
android:layout_alignTop="@id/text1"
//對齊父控件的四個方向
android:layout_alignParentBottom=“true”
android:layout_alignParentTop=“true”
android:layout_alignParentLeft=“true”
android:layout_alignParentRight=“true”
//位于父控件中心(正中心、水準中心、垂直中心)
android:layout_centerInParent=“true”
android:layout_centerHorizontal=“true”
android:layout_centerVertical=“true”
【布局嵌套】
一個布局可以成為另一個布局的子布局,内部的布局整體相當于外部的布局中的一個控件。
從理論上講,嵌套可以任意層,但是在實際開發中建議能不嵌套就不嵌套,因為嵌套會降低程式中的執行效率。
【比例劃分】
可以使用線性布局+權重屬性兩者配合實作控件寬度和高度的比例。
如果線性布局是橫向的,那麼隻能分子控件的寬度;
如果線性布局是縱向的,那麼隻能分子控件的高度。
上面建議自己想想,不建議死記。
建議給寬度設定權重就把寬度設定為0dp,
建議給高度設定權重就把高度設定為0dp。
//權重
android:layout_weight=“1”
【對齊代碼】
Eclipse Ctrl + Shift + F,前提是不要使用國産輸入法。
AS Ctrl + Alt + L