线性布局:LinearLayout
相对布局:RelativeLayout
帧布局:FrameLayout
表格布局:TableLayout
网格布局:GridLayout
约束布局:ConstraintLayout
添加布局方式:1.利用xml设计方式,2.采用java代码添加
布局添加例子: 这个例子平常使用较为麻烦,维护难度也大,这个做逻辑控制较为简单
1.指定根布局为线性布局 Linearlayout q=new LinearLayout(this);this表示当前的对象,MainActivity
2.设置宽高
setLayoutParams(new Linearlayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT))
3.设置背景颜色 (红色) setBackGroundColor(Color.RED)
4.指定此次Activity的内容视图为该线性布局 setContentView(q)
布局属性:
android:layout_width 宽度 match_parent匹配父容器,即与窗口大小相同 dp是象素的单位,常在xml文件里使用
android:layout_height 高度
android:layout_padding 内边距
android:layout_margin 外边距
线性布局重要属性 sp是字体大小的单位
android:orientation 方向(默认是水平的,但是不要不编写) vertical垂直的 horizontal水平的
android:layout_weight 权重 1.设置某些特殊效果的布局,可以使所在布局文件占据剩余所有位置 2.按比例划分空间,需要先把所调的宽或高改为0dp
android:layout_gravity 重力 表示偏向哪里,可以是上下左右中
android:textSize 表示字体大小