我個人比較容易混淆的是前兩個:
android:gravity: 屬性是對該view中內容的限定.比如一個button 上面的text. 你可以設置該text 相對於view的靠左,靠右等位置.
android:layout_gravity:是用來設置該view相對與父view 的位置.比如一個button 在linearlayout裏,你想把該button放在linearlayout裏靠左、靠右等位置就可以通過該屬性設置。
即android:gravity用於設置View中內容相對於View組件的對齊方式,而android:layout_gravity用於設置View組件相對於Container的對齊方式
android:layout_paddingLeft 內邊距,對誰用,指的是誰的內部內容邊距
android:layout_marginLeft外邊距,對誰用,指的是誰距離外層容器的邊距
如:
當按鈕分別設置以上兩個屬性時,得到的效果是不一樣的。
android:paddingLeft="30px":
按鈕上設置的內容(例如圖片)離按鈕左邊邊界30個像素。
android:layout_marginLeft="30px"
整個按鈕離左邊設置的內容30個像素
這二個屬性是相對的,假設B是A的子控件,設置B的margin和設置A的padding能達到相同的效果
在 android 中我們常用的佈局方式有這麼幾種:
1.LinearLayout ( 線性佈局 ) :(裏面只可以有一個控件,並且不能設計這個控件的位置,控件會放到左上角)
線性佈局分爲水平線性和垂直線性二者的屬性分別爲: android:orientation= " horizontal " android:orientation= "vertical" 。
2.RelativeLayout ( 相對佈局 ) : (裏面可以放多個控件,但是一行只能放一個控件)
附加幾類 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 離某元素上邊緣的距離
3.TableLayout ( 表格佈局 ) : (這個要和TableRow配合使用,很像html裏面的table)
這個表格佈局不像HTML中的表格那樣靈活,只能通過 TableRow 屬性來控制它的行而列的話裏面有幾個控件就是幾列(一般情況)。 如:
<TableLayout>
<TableRow>
<EditText></EditText>
<EditText></EditText>
</TableRow>
<TableRow>
<EditText></EditText>
<EditText></EditText>
</TableRow>
</TableLayout>
表示兩行兩列的一個表格。
android:gravity="center" 書面解釋是權重比。其時就是讓它居中顯示。它還可以動態添加里面的每行每列。如下代碼所示:
/*根據id查找表格對象*/
TableLayout tableLayout = (TableLayout) findViewById(R.id.table01);
/*創建列對象*/
TableRow tableRow = new TableRow(this);
/*文本框對象*/
TextView temp = new TextView(this);
temp.setText("text的值");
/*將此文本添加到列中*/
tableRow.addView(temp);
android:stretchColumns="1,2,3,4" 它的意思就是自動拉伸1,2,3,4列。
4.AbsoluteLayout ( 絕對佈局 ) : (裏面可以放多個控件,並且可以自己定義控件的x,y的位置)
5.FrameLayout ( 幀佈局 ) :(裏面可以放多個控件,不過控件的位置都是相對位置)
在它裏面的控件都是按後面的一個控件疊加在前一個控件上來顯示的,所有元素都被放置在最左上角。 如:
<FrameLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1">
<ImageView android:id="@+id/iv1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:visibility="invisible"
android:src="@drawable/lotusleaf"></ImageView>
<ImageView android:id="@+id/f1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/frog_right"
android:visibility="invisible"></ImageView>
</FrameLayout>
表示的是id爲f1的控件疊加在id爲iv1的控件上面顯示
(LinearLayout 和 RelativeLayout 應該又是其中用的較多的兩種。AbsoluteLayout 比較少用,因爲它是按屏幕的絕對位置來佈局的如果屏幕大小發生改變的話控件的位置也發生了改變。這個就相當於HTML中的絕對佈局一樣,一般不推薦使用 )
android通用屬性
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文件夾裏面的圖片命名是不能大寫的。
android:id
爲控件指定相應的ID
android:text
指定控件當中顯示的文字,需要注意的是,這裏儘量使用strings.xml文件當中的字符串
android:gravity
指定View組件的對齊方式,比如說居中,居右等位置 這裏指的是控件中的文本位置並不是控件本身
android:layout_gravity
指定Container組件的對齊方式.比如一個button 在linearlayout裏,你想把該button放在靠左、靠右等位置就可以通過該屬性設置.以button爲 例,android:layout_gravity="right"則button靠右
android:textSize
指定控件當中字體的大小
android:background
指定該控件所使用的背景色,RGB命名法
android:width
指定控件的寬度
android:height
指定控件的高度
android:layout_width
指定Container組件的寬度
android:layout_height
指定Container組件的高度
android:layout_weight
View中很重要的屬性,按比例劃分空間
android:padding*
指定控件的內邊距,也就是說控件當中的內容
android:sigleLine
如果設置爲真的話,則控件的內容在同一行中進行顯示
android:scaleType
是控制圖片如何resized/moved來匹對ImageView的siz
android:layout_centerHrizontal
水平居中
android:layout_centerVertical
垂直居中
android:layout_centerInparent
相對於父元素完全居中
android:layout_alignParentBottom
貼緊父元素的下邊緣
android:layout_alignParentLeft
貼緊父元素的左邊緣
android:layout_alignParentRight
貼緊父元素的右邊緣
android:layout_alignParentTop
貼緊父元素的上邊緣
android:layout_alignWithParentIfMissing
如果對應的兄弟元素找不到的話就以父元素做參照物
android:layout_below
在某元素的下方
android:layout_above
在某元素的的上方
android:layout_toLeftOf
在某元素的左邊
android:layout_toRightOf
在某元素的右邊
android:layout_alignTop
本元素的上邊緣和某元素的的上邊緣對齊
android:layout_alignLeft
本元素的左邊緣和某元素的的左邊緣對齊
android:layout_alignBottom
本元素的下邊緣和某元素的的下邊緣對齊
android:layout_alignRight
本元素的右邊緣和某元素的的右邊緣對齊
android:layout_marginBottom
離某元素底邊緣的距離
android:layout_marginLeft
離某元素左邊緣的距離
android:layout_marginRight
離某元素右邊緣的距離
android:layout_marginTop
離某元素上邊緣的距離
android:paddingLeft
本元素內容離本元素右邊緣的距離
android:paddingRight
本元素內容離本元素上邊緣的距離
android:hint
設置EditText爲空時輸入框內的提示信息
android:orientation
它確定了Layout的方向,其值可以爲vertical, 表示垂直佈局horizontal, 表示水平佈局
-----------------------------------------------------------------------------------------------------------------------------------------------------
android:interpolator
可能有很多人不理解它的用法,文檔裏說的也不太清楚,其實很簡單,看下面:interpolator定義一個動畫的變化率(the rate of change)。這使得基本的動畫效果(alpha, scale, translate, rotate)得以加速,減速,重複等。用通俗的一點的話理解就是:動畫的進度使用 Interpolator 控制。interpolator 定義了動畫的變化速度,可以實現勻速、正加速、負加速、無規則變加速等。Interpolator 是基類,封裝了所有 Interpolator 的共同方法,它只有一個方法,即
getInterpolation (float input),該方法 maps a point on the timeline to a multiplier to be applied to the transformations of an animation。Android 提供了幾個 Interpolator 子類,實現了不同的速度曲線,如下:
AccelerateDecelerateInterpolator 在動畫開始與介紹的地方速率改變比較慢,在中間的時侯加速
AccelerateInterpolator 在動畫開始的地方速率改變比較慢,然後開始加速
CycleInterpolator 動畫循環播放特定的次數,速率改變沿着正弦曲線
DecelerateInterpolator 在動畫開始的地方速率改變比較慢,然後開始減速
LinearInterpolator 在動畫的以均勻的速率改變
對於 LinearInterpolator ,變化率是個常數,即 f (x) = x.
public float getInterpolation(float input) {
return input;
}
Interpolator其他的幾個子類,也都是按照特定的算法,實現了對變化率。還可以定義自己的 Interpolator 子類,實現拋物線、自由落體等物理效果。