<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:text="紅色" android:gravity="center_horizontal|center_vertical" android:background="#aa0000" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="綠色" android:gravity="center_horizontal" android:background="#00aa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="藍色" android:gravity="center|bottom" android:background="#0000aa" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="黃色" android:gravity="bottom" android:background="#aaaa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:text="第一行" android:textSize="15sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="第二行" android:textSize="15sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="第三行" android:textSize="15sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="第四列" android:textSize="15sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#0000FF" android:padding="10px" > <TextView android:id="@+id/tv01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="30dp" android:text="Type here:" /> <EditText android:id="@+id/txt01" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tv01" /> <Button android:id="@+id/btn01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="@id/txt01" android:layout_alignParentRight="true" android:text="OK" /> <Button android:id="@+id/btn02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="@id/txt01" android:layout_toLeftOf="@id/btn01" android:layout_marginRight="30dp" android:text="Cancel" /></RelativeLayout>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView android:id="@+id/id_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="id:" > </TextView> <EditText android:id="@+id/id_editText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="true" > </EditText> <TextView android:id="@+id/name_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name:" > </TextView> <EditText android:id="@+id/name_editText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="true" > </EditText> <TextView android:id="@+id/sno_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="sno:" > </TextView> <EditText android:id="@+id/sno_editText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="true" > </EditText> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/createButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dip" android:text="創建" > </Button> <Button android:id="@+id/insertButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/createButton" android:text="插入" > </Button> <Button android:id="@+id/queryButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/createButton" android:text="查詢全部" > </Button> <Button android:id="@+id/queryButtonSomeOne" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/queryButton" android:text="單查詢" > </Button> <Button android:layout_width="wrap_content" android:text="更新" android:layout_height="wrap_content" android:id="@+id/updateButton" android:layout_toRightOf="@+id/queryButtonSomeOne" > </Button> <Button android:layout_width="wrap_content" android:text="刪除" android:layout_height="wrap_content" android:id="@+id/deleteButton" android:layout_toRightOf="@+id/updateButton" > </Button> </RelativeLayout> <TextView android:id="@+id/displayResult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="display result..." android:layout_gravity="center_horizontal" > </TextView>
RelativeLayout用到的一些重要的屬性:
第一類:屬性值為true或false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相對於父元素完全居中
android:layout_alignParentBottom 貼緊父元素的下邊緣
android:layout_alignParentLeft 貼緊父元素的左邊緣
android:layout_alignParentRight 貼緊父元素的右邊緣
android:layout_alignParentTop 貼緊父元素的上邊緣
android:layout_alignWithParentIfMissing 如果對應的兄弟元素找不到的話就以父元素做參照物
第二類:屬性值必須為id的引用名「@id/id-name」
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左邊
android:layout_toRightOf 在某元素的右邊
android:layout_alignTop 本元素的上邊緣和某元素的的上邊緣對齊
android:layout_alignLeft 本元素的左邊緣和某元素的的左邊緣對齊
android:layout_alignBottom本元素的下邊緣和某元素的的下邊緣對齊
android:layout_alignRight 本元素的右邊緣和某元素的的右邊緣對齊
第三類:屬性值為具體的像素值,如30dip,40px
android:layout_marginBottom 離某元素底邊緣的距離
android:layout_marginLeft 離某元素左邊緣的距離
android:layout_marginRight 離某元素右邊緣的距離
android:layout_marginTop 離某元素上邊緣的距離
EditText的android:hint
設置EditText為空時輸入框內的提示信息。
android:gravity
android:gravity屬性是對該view 內容的限定.比如一個button上面的text. 你可以設置該text在view的靠左,靠右等位置.以button為例,android:gravity="right"則button上面的文字靠右
android:layout_gravity
android:layout_gravity是用來 設置該view相對與起父view 的位置.比如一個button在linearlayout裡,你想把該button放在靠左、靠右等位置就可以通過該屬性設置.以button為例,android:layout_gravity="right"則button靠右
android:layout_alignParentRight
使當前控制項的右端和父控制項的右端對齊。這裡屬性值只能為true或false,默認false。
android:scaleType:
android:scaleType是控制圖片如何resized/moved來匹對ImageView的size。ImageView.ScaleType/ android:scaleType值的意義區別:
CENTER /center 按圖片的原來size居中顯示,當圖片長/寬超過View的長/寬,則截取圖片的居中部分顯示
CENTER_CROP / centerCrop 按比例擴大圖片的size居中顯示,使得圖片長(寬)等於或大於View的長(寬)
CENTER_INSIDE / centerInside 將圖片的內容完整居中顯示,通過按比例縮小或原來的size使得圖片長/寬等於或小於View的長/寬
FIT_CENTER / fitCenter 把圖片按比例擴大/縮小到View的寬度,居中顯示
FIT_END / fitEnd 把圖片按比例擴大/縮小到View的寬度,顯示在View的下部分位置
FIT_START / fitStart 把圖片按比例擴大/縮小到View的寬度,顯示在View的上部分位置
FIT_XY / fitXY 把圖片不按比例擴大/縮小到View的大小顯示
MATRIX / matrix 用矩陣來繪製,動態縮小放大圖片來顯示。
** 要注意一點,Drawable文件夾裡面的圖片命名是不能大寫的。