android布局tips,基礎知識搜集
本文不斷更新,用來記錄平時編寫布局相關的有用的點,這裡共用給大家,如有錯誤懇請指出,謝謝。
1、LIstView下面添加按鈕等東西,可以設定listView的屬性android:layout_weight="1" 然後就可以顯示了。
2、倆個Button填充滿一行,可以設定屬性android:layout_weight="1" 。可以平分顯示了。
3、android:gravity和android:layout_gravity的區別:
android:gravity 是用於子控制項在當前控制項中的排列位置
android:layout_gravity 是用於當前控制項在父控制項中的排列位置
例如:一個控制項button: android:layout_gravity 表示按鈕在介面上的位置。 android:gravity表示button上的字在button上的位置。
4、RelativeLayout一些重要的屬性,通過下面屬性可以實現很多布局效果
android:layout_centerInParent 置中布局
android:layout_centerVertical 垂直置中布局
android:layout_centerHorizontal 水平置中布局
android:layout_alignParentTop 居於容器內頂部
android:layout_alignParentBottom 居於容器內底部
android:layout_alignParentLeft 居於容器內左邊
android:layout_alignParentRight 居於容器內右邊
android:layout_above 居於指定View的上方
android:layout_below 居於指定View的下方
android:layout_toRightOf 在指定View的右邊
android:layout_toLeftOf 在指定View的左邊
android:layout_alignTop 與指定View的Top一致
5、Android TextView中有個內容過長加省略符號的屬性,即ellipsize,在xml中用法如下:
android:ellipsize = "end" 省略符號在結尾
android:ellipsize = "start" 省略符號在開頭
android:ellipsize = "middle" 省略符號在中間
android:ellipsize = "marquee" 跑馬燈
android:singleline = "true"
6、Android button設定點擊前後顏色不一樣
在res目錄下建立一個drawable檔案夾,這個檔案夾是和解析度無關的,然後在這個檔案夾裡面建立一個button_selector.xml檔案,根節點是selector喔。
建立好後,修改整個檔案內容,變成如下所示:
然後在res/values目錄下建立colors.xml檔案,內容如下:
#2464A9 #0099FF
然後在布局檔案中,找到需要添加效果的button,添加如下屬性:
android:background="@drawable/button_selector"
好了,我們需要的效果就完成了。