Android基礎之佈局篇
工作之餘,整理一下自己所掌握的Android知識,希望能對學習Android開發的小夥伴有所幫助: android 有五種佈局: >Linearlayout 線性佈局 >RelativeLayout 相對佈局 >FrameLayout 幀佈局 >TableLayout 表格佈局(過時) >AbsoluteLayout 絕對佈局(過時)
4.0以後 新增了一個佈局:GridLayout
1,View和ViewGroup >View 在UI介面 佔矩形介面 >ViewGroup 不可見的 容器
2.LinearLayout線性佈局******** >概念:單一方向的佈局 按水平方向或者垂直方向 所有子元素 在水平方向或者垂直方向 每一行或者每一列只能有一個元素
>屬性: >1,LinearLayout的方向屬性 orientation android:orientation:方向屬性 有倆個方向 horizontal(水平),vertical(垂直) android:orientation="horizontal" android:orientation="vertical" LinearLayout 預設是有方向的 水平方向 >2,權重屬性 android:layout_weight="1" 權重屬性 分配父控制元件的剩餘空間 剩餘空間:父控制元件的寬或者高 減去 子控制元件 (所有子元素)的寬或者高 (*和方向有關) 分配:子控制元件的寬或者高 = 原有的寬度或者高度 + 剩餘空間(寬或者高)/子控制元件權重的和 *子控制元件的權重
note :使用android:layout_weight時設定子控制元件的寬或者高為0dp(成比例進行分配)
>3, android:layout_gravity="bottom" 相對於父控制元件的位置 android:layout_gravity屬性和父控制元件的方向有關 如果父控制元件是水平方向android:layout_gravity的自身的水平方向屬性是無效的 如果父控制元件是垂直方向 android:layout_gravity的自身的垂直方向屬性是無效的
>4,android:gravity="bottom|right" 如果當前屬性是在佈局節點:子元素相對於自身的位置 如果當前屬性在控制元件中 (button ,TextView..)控制的是控制元件裡的內容 (文字) 3.RelativeLayout相對佈局********* >概念:根據控制元件的相對位置 進行佈局
>屬性: 16個 >1,相對於父控制元件的位置 1,居中位置 android:layout_centerInParent="true"相對於父控制元件的中央位置 android:layout_centerHorizontal="true"相對於父控制元件的水平居中 android:layout_centerVertical="true"相對於父控制元件的垂直居中 水平居中和垂直居中 可以重複使用 2,對齊位置 android:layout_alignParentTop="true" 相對於父控制元件的 上對齊 android:layout_alignParentBottom="true" 相對於父控制元件的 底邊對齊 android:layout_alignParentRight="true" 相對於父控制元件的 右對齊 android:layout_alignParentLeft="true" 相對於父控制元件的 左對齊
>2,相對兄弟控制元件的位置 >1,相對兄弟元素的位置 android:layout_above="@+id/tv1" 在指定兄弟控制元件的上邊(頂部) android:layout_below="@+id/tv1" 在指定兄弟控制元件的下邊(底邊) android:layout_toLeftOf="@+id/tv1"在指定兄弟控制元件的左邊 android:layout_toRightOf="@+id/tv1"在指定兄弟控制元件的右邊
>2, 相對於兄弟控制元件的對齊方式 : android:layout_alignTop="@id/tv_money" 相對指定兄弟控制元件的頂部對齊 android:layout_alignBottom="@id/tv_money"相對指定兄弟控制元件的底部對齊 android:layout_alignLeft="@id/tv_money" 相對指定兄弟控制元件的左側對齊 android:layout_alignRight="@id/tv_money" 相對指定兄弟控制元件的右側對齊 android:layout_alignBaseline="@id/tv_money"相對指定兄弟控制元件的基準線對齊
瞭解:
4,Framelayout:幀佈局 >概念:每個元件都是一幀 當前子元件會覆蓋前一個元件
>屬性: android:layout_gravity="center" 控制 當前元件的位置 android:foreground="@drawable/ic_launcher"放置在佈局管理器所有元件之上 android:foregroundGravity="top|right" 控制放置的位置 屬性可以用"|" 疊加使用 屬性不能互斥 5,GridLayout:網格佈局
android:orientation="vertical" 排列方式 android:rowCount="3" 行的數量 android:columnCount="3"列的數量 位置是從0開始 android:layout_row="0" 控制元件在第幾行展示 android:layout_column="0" 控制元件在第幾列展示 android:layout_columnSpan="2"列的跨度 android:layout_rowSpan="2" 行的跨度 android:layout_gravity="fill_horizontal" 填充所誇的行和列 fill 水平和垂直方向都填充 fill_horizontal 水平方向填充 fill_vertical 垂直方向填充
4,快捷鍵:
shift+ctrl+/ 註釋 ctrl+alt+下的方向鍵 複製 ctrl+shift+f 格式化程式碼
5,顏色
背景 :可以用顏色和圖片填充 顏色:#RGB #RRGGBB #AARRGGBB A代表透明度 android:background="#ff0000"
6, @+id:R檔案中沒有 @id :R檔案中存在