目前正在從事iOS開發,對于安卓就是大學的時候自學了點,做過幾個小的項目,軟件外包大賽、計算機設計大賽、移動應用大賽都拿過獎項,呵呵。。。現在回想起來以前大學做的安卓比賽是多么的幼稚。 從現在開始我要從頭一步一步回顧安卓,作為一個前端開發工程師要崛起了。。。。哈哈。。。請大家支持的我創作,多提提寶貴的意見。
步入正題!!!!!!!
關于Android的幾種常見的布局其實很簡單的,是一個入門的關鍵。主要的線性布局(所有的ui控件都是 以一條線 線性的 方式去 排列的)、相對布局(所有的控件 都是 以相對的位置去放置)、其次表格布局、幀布局(就是頁面的疊加,播放器里經常用到)、絕對布局(已廢棄)。
關于布局就是xml文件里寫。可能有人會說那些標簽太多不好記,沒問題,不會的可以去控件里拖拽,時間長了子讓會記住了。我就拿例子來說吧。
線性布局寫的一個小米計算器
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:text="C"
android:textColor="#FFC928"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"/>
android:text="DEL"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"/>
android:text="÷"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"/>
android:text="×"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:text="7"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"/>
android:text="8"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"/>
android:text="9"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"/>
android:text="—"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:text="4"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"/>
android:text="5"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"/>
android:text="6"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"/>
android:text="+"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="0dip"
android:layout_weight="3"
android:layout_height="wrap_content"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:text="1"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"/>
android:text="2"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"/>
android:text="3"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:text="0"
android:layout_width="0dip"
android:layout_weight="2"
android:layout_height="wrap_content"/>
android:text="."
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"/>
android:text="="
android:background="#F07A23"
android:layout_width="0dip"
android:layout_weight="1"
android:gravity="bottom|right"
android:layout_height="fill_parent"/>
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton" />
線性布局與相對布局混合使用做一個設置界面
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#113965"
android:orientation="vertical"
tools:context=".MainActivity" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="自動旋轉屏幕"
android:textColor="#ffffff" />
android:id="@+id/cbx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#ff0000" />
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:visibility="visible" />
網格布局做的類似qq的登入頁面
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:text="請輸入QQ"
android:layout_width="0dip"
android:layout_weight="2"
android:layout_height="wrap_content"/>
android:layout_width="0dip"
android:layout_weight="5"
android:layout_height="wrap_content"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:text="請輸入QQ密碼"
android:layout_width="0dip"
android:layout_weight="2"
android:layout_height="wrap_content"/>
android:layout_width="0dip"
android:layout_weight="5"
android:layout_height="wrap_content"/>
android:text="登入"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
絕對布局做的類似游戲鼠標的界面
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@+id/middle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:text="中間" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/middle"
android:layout_centerHorizontal="true"
android:text="上" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/middle"
android:layout_centerHorizontal="true"
android:text="下" />
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/middle"
android:text="左" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/middle"
android:text="右" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左上角" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="14dp"
android:text="左下角" />
幀布局
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:text=""
android:background="#ff0000"
android:layout_width="200dip"
android:layout_height="200dip"/>
android:text=""
android:background="#00ff00"
android:layout_width="100dip"
android:layout_height="100dip"/>
android:text=""
android:background="#0000ff"
android:layout_width="50dip"
android:layout_height="50dip"/>