Android六大布局的基本使用
1、LinearLayout(线性布局)
LinearLayout主要属性:orientation(排列方向) weight(权重)
orientation(排列方向) :主要分horizontal(水平)和vertical(垂直)
weight(权重):主要用在0dp、wrap_content、match_parent情况下。
horizontal情况下,设置内部view的width为0dp或wrap_content,然后通过weight设置比例,vertical同理;
horizontal情况下,设置内部view的width为match_parent,然后通过weight设置比例(计算方法,如1:2:3,比例就是1/6,2/6,3/6,每项计算类似1-2*(1/6),结果是2:1:0 ),vertical同理;
假设width=480 父控件剩余宽度rest=480-480*3(分三块)=-480*2
计算方法:控件宽度+父控件剩余宽度*比例
tv1=480-(480*2)*(1/6)=480*(4/6)
tv2=480-(480*2)*(2/6)=480*(2/6)
tv3=480-(480*2)*(3/6)=480*(0/6)
例子:
2、RelativeLayout(相对布局)
根据父容器定位或兄弟组件定位
RelativeLayout主要属性:
layout_centerInParent(居中)
layout_centerHorizontal(水平居中) layout_centerVertical(垂直居中)
layout_above(控件上方)layout_below(控件下方)
layout_toLeftOf (控件左边) layout_toRightOf(控件右边)
注意:margin是偏移,针对容器中的组件;padding是填充,针对组件中的元素
简单来说margin为外边框,border为边框,padding为内边框
例子:
3、TableLayout(表格布局)
TableLayout主要属性:
collapseColumns(隐藏列) stretchColumns(拉伸列) shrinkColumns(收缩列)
例子:
4、FrameLayout(帧布局)
FrameLayout主要属性:foreground(前景图像) foregroundGravity(前景图像位置)
例子:
5、GridLayout(网格布局)
GridLayout主要属性:
columnCount rowCount(布局行列数) layout_columnSpan layout_rowSpan(组件横跨)
例子:
6、AbsoluteLayout(绝对布局)
AbsoluteLayout主要属性:layout_x和layout_y
例子:
https://github.com/xian131/LittleDemoAndroid/tree/master/Test01Layout