ViewGroup
ViewGroup.LayoutParams | 作用 |
---|---|
android:layout_height | 控制佈局的高度 |
android:layout_width | 控制佈局的寬度 |
FILL-RARENT | 與父容器相同 |
MATCH_PARENT | 與父容器相同 |
WARP_CONTENT | 包裹自身的內容 |
ViewGroup.MarginLayoutParams | 控制子組件的外邊距 |
外邊距:指組件四周的區域
- android:layout_marginTop
- android:layout_marginLeft
- android:layout_marginBottom
- android:layout_marginRight
內邊距:
- android:paddingTop
- android:paddingLeft
- android:paddingBotton
- android:paddingRight
具體值可以通過具體屬性值來設置;也可以通過屬性變量來設置
使用XML佈局文件控制UI界面基本步驟
- 在Android應用的res/layout目錄下面編寫XML佈局文件
- 在Activitiy中使用Java代碼顯示XML文件中佈局的內容
setContentView(R.layout.activity_main);
相對佈局
如何定義相對佈局RelativeLayout
RelativeLayout.LayoutParams提供一些xml屬性
RelativeLayout.LayoutParams提供一些xml屬性 | 作用 |
---|---|
android:gravity | 各子組件擺放方式 |
android:ignoreGravity | 那個組件不受上個組件影響 |
android:layout_above | 表示當前控件佈局在選擇id控件的上面 |
android:layout_below | 表示當前控件佈局在選擇id控件的下面 |
android:layout_toLeftOf | 表示當前控件佈局在選擇id控件的左邊 |
android:layout_toRightOf | 表示當前控件佈局在選擇id控件的右邊 |
android:layout_alignparentBottom | 貼緊父元素的下邊緣 |
android:layout_alignparentLeft | 貼緊父元素的左邊緣 |
android:layout_alignparentRight | 貼緊父元素的右邊緣 |
android:layout_alignparentTop | 貼緊父元素的上邊緣 |
android:layout_alignBottom | 該控件的底部和指定ID的控件的底部對齊 |
android:layout_alignLeft | 該控件的底部和指定ID的控件的左邊緣對齊 |
android:layout_alignRight | 該控件的底部和指定ID的控件的右邊緣對齊 |
android:layout_alignTop | 該控件的底部和指定ID的控件的頂部對齊 |
android:layout_centerHorizotal水平居中 | |
android:layout_centerInParent | 相對於父元素完全居中 |
android:layout_centerVertical | 垂直居中 |
線性佈局管理器(LinearLayout)
android:orientation="vertical"//垂直
//每一行只能放置一個組件,並且這個組件也不會換行,可能有不被顯示
android:orientation="horizontal"//水平
//每一列只能放置一各組件,並且這個組件也不會換列,可能有不被顯示
如何定義線性佈局管理器
LinearLayout標記 | 作用 |
---|---|
android:orientation | 指定佈局內控件排列方式 |
android:gravity | 控件佈局方式 |
android:layout_weight | 設置組件所佔權重 |
幀佈局管理器
主要顯示重疊的內容或者拖動效果
FrameLayout標記 | 作用 |
---|---|
android:foreground: | 爲當前的幀佈局管理器設置一個前景圖像 |
android:foregroundGravity | 設置前景圖像的位置 |
表格佈局管理器
以行列的形式來管理放入UI的組件
TableLayout | 作用 |
---|---|
TableRow | 每添加一個,就多一行 |
android:collapseColumns=“n” | 隱藏n+1行 |
android:stretchColumns=“n” | 拉伸n+1列 |
android:shrinkColumns=“n” | 收縮n+1列 |
網格佈局管理器
GridLayout | 作用 |
---|---|
android:columnCount | 指定網格最大列數 |
android:orientation | 指定當我們沒有爲放置器組件分配行和列時的排列方式 |
android:rowCount | 指定網格最大行數 |
GridLayout.LayoutParams | 作用 |
---|---|
android:layout_column | 指定子組件位於網格的第幾列 |
android:layout_columnSpan | 指定子組件橫向跨幾列 |
android:layout_columnWeight | 指定子組件在水平方向的權重 |
android:layout_gravity | 設置子組件採用什麼方式佔據網格的空間 |
android:layout_row | 指定子組件位於網格的第幾行 |
android:layout_rowSpan | 指定子組件縱向跨幾行 |
android:layout_rowWeight | 在垂直方向上的權重 |