margin 外邊距android
padding 內邊距函數
layout_ 稱之爲 佈局參數,用於告訴父組件如何安排自身的子元素(margin,告訴處理組件間距)佈局
不帶layout 做用於自身(padding,組件大於文字多少)spa
dp 與密度無關的像素 density independent pixelcode
sp 與縮放無關的像素 scale (比例尺) independent pixelorm
layout_weight 當layout_width 於layout_height 分配好空間後,剩餘空間,layout_weight用做分配組件所佔空間份額的加權值(如要徹底按照weight分配,只需置其餘參數爲0)xml
使用DateFormat類格式化date對象
String formatDate=(String)DateFormat.format("E,MMMM,yy",mCrime.getDate());
"MM/dd/yy h:mmaa" -> "11/03/87 11:23am"blog
"MMM dd, yyyy h:mmaa" -> "Nov 3, 1987 11:23am"ip
"MMMM dd, yyyy h:mmaa" -> "November 3, 1987 11:23am"
"E, MMMM dd, yyyy h:mmaa" -> "Tues , November 3, 1987 11:23am"
"EEEE, MMMM dd, yyyy h:mmaa" -> "Tues day, Nov 3, 1987 11:23am"
"YYYY年MM月dd日,kk:mm" -> 2014年09月30日,11:23
"EEEE, MMMM dd, yyyy kk:mm" -> "Tues day, Nov 3, 1987 23:23"
其中:12小時制 :hh; 24小時制: kk,若是用 SimpleDateFormat ,則爲: hh; HH
Android.text.format.DateFormat類的static boolean is24HourFormat(Context context)方法能夠用來判斷當前系統時間是否爲24小時制式
用一個私有的空的構造函數,使其餘函數沒法創造新的對象,只能使用get
···
public class CrimeLab { private static CrimeLab sCrimeLab; public static CrimeLab get(Context context){ if(sCrimeLab == null){ sCrimeLab = new CrimeLab(context) } return sCrimeLab; } private CrimeLab(Context context){ } }
···FrameLayout
FrameLayout應該是Android系統中最簡單的佈局了,在FrameLayout中的元素,默認都是以FrameLayout控件的坐上頂點做爲基準點,一層一層的重疊起來,後加進來的元素覆蓋前面的元素。
下面先來一個演示,代碼以下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <View android:layout_width="200dp" android:layout_height="200dp" android:background="#ff0000"/> <View android:layout_width="200dp" android:layout_height="200dp" android:background="#000000"/> <View android:layout_width="200dp" android:layout_height="200dp" android:layout_margin="100dp" android:background="#00ff00"/> </FrameLayout>
運行結果以下:
在代碼裏,有三個View,而在運行結果上只能看到兩個View,一個黑色和一個綠色。這是由於紅色的View被黑色的View蓋住了。
在FrameLayout中,經過android:layout_gravity屬性去指定子元素的位置,下面調整一下上訴例子中的黑色View的位置,讓紅色的View顯示出來,調整後的代碼以下:
<View android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="bottom|right" android:background="#000000"/>
能夠看到上面代碼裏添加了android:layout_gravity屬性,而且指定了兩個值,一個爲bottom,一個爲right,表示這個View將被放到FrameLayout的右下角。運行結果以下圖所示:
LinearLayout是線性佈局,它可讓它內部的元素按照指定方向依次排開。LinearLayout的方向是經過android:orientation屬性指定,而且能夠經過android:gravity屬性指定對其方式。
仍是直接上段代碼看看效果,代碼以下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <View android:layout_width="100dp" android:layout_height="100dp" android:background="#ff0000"/> <View android:layout_width="100dp" android:layout_height="100dp" android:background="#000000"/> <View android:layout_width="100dp" android:layout_height="100dp" android:background="#00ff00"/> </LinearLayout>
在代碼中,設置了LinearLayout的方向爲縱向,而且對其方式居中對齊,因而運行結果以下圖所示:
除了android:orientation將設爲vertical外,也能夠設爲horizontal。讓LinearLayout內部的元素橫向排列,將上面例子中的android:orientation屬性值改成horizontal後的運行結果,以下圖所示:
RelativeLayout是基本佈局裏面最靈活,也是最複雜的佈局,它內部的元素能夠經過設定彼此之間的相對關係來決定佈局,使用RelativeLayout時,推薦爲其內部每一個元素都設定id,下面依然經過一個列子來演示此佈局的使用方法。代碼以下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <View android:id="@+id/red" android:layout_width="100dp" android:layout_height="100dp" android:background="#ff0000"/> <View android:id="@+id/black" android:layout_width="100dp" android:layout_height="100dp" android:layout_toRightOf="@id/red" android:layout_below="@id/red" android:background="#000000"/> <View android:id="@+id/green" android:layout_width="100dp" android:layout_height="100dp" android:layout_below="@id/black" android:layout_alignParentRight="true" android:background="#00ff00"/> <View android:id="@+id/gray" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerInParent="true" android:background="#888888"/> <View android:id="@+id/orange" android:layout_width="100dp" android:layout_height="100dp" android:layout_toRightOf="@id/green" android:layout_below="@id/gray" android:background="#ff8800"/> </RelativeLayout>
先分析代碼,能夠看到每個View都被設置了一個id值,分別爲red,black,green,gray和orange。而後經過代碼,能夠看出black位於red的右邊和下面,green位於black的下面而且右對齊其父元素(即RelativeLayout),gray居中對齊父元素(即RelativeLayout), orange位於green的右邊同時位於gray的下面,運行結果如圖所示:
在此在概括一下RelativeLayout中,與佈局相關的屬性:
android:layout_below:位於指定元素的下方
android:layout_above:位於指定元素的上方
android:layout_toLeftOf:位於指定元素的左側
android:layout_toRightOf:位於指定元素的右側
android:layout_centerVertical:垂直居中對齊父元素
android:layout_centerHorizontal:水平居中對齊父元素
android:layout_centerInParent:居中對齊父元素
android:layout_alignParentRight:與父元素右對齊
android:layout_alignParentLeft:與父元素左對齊
android:layout_alignParentTop:與父元素上對齊
android:layout_alignParentBottom:與父元素下對齊
android:layout_alignRight:與指定元素右對齊 android:layout_alignLeft:與指定元素左對齊
android:layout_alignTop:與指定元素上對齊 android:layout_alignBottom:與指定元素下對齊
從Android 4.2開始,也就是從API Level 17開始,Android加強了RelativeLayout,使其可以更好的應對並本地化這一需求,好比在有的國家,文字是從右往左閱讀,這也就是所說的RTL。爲了應對RTL,RelativeLayout又增長了如下屬性:
android:layout_alignStart:與指定元素的開始位置對齊
android:layout_toStartOf:位於指定元素的開始側
android:layout_alignParentStart:與父元素與開始側對齊
android:layout_alignEnd:與指定元素的結束始位置對齊
android:layout_toEndOf:位於指定元素的結束側
android:layout_alignParentEnd:與指定元素的結束位置對齊
這裏的開始和結束咱們能夠作以下理解:
開始:在從左到右閱讀習慣的國家,開始側等於左側,toStartOf的顯示效果就等於toLeftOf。可是在從右往左閱讀習慣的國家,那麼開始側就變成了右側,toStartOf的顯示效果就等於了toRightOf。結束:同上面對開始的理解同樣,結束側在從左到右閱讀習慣的國家就是右側,反之則在左側。