标题view视图
android中所有的UI都是通过View和ViewGroup构建的,ViewGroup作为容器装界面中的控件,它可以包含View和ViewGroup,具体关系如下:
注意:android应用的每个界面的根元素必须有且只有一个ViewGroup容器。
标题界面布局的编写:
android中用XML文件控制界面布局,也可以和java代码分开,使得代码结构清晰。【res/layout】文件位置。
Java代码也可以编写代码,android中所有的布局和控件的对象都可以通过new 关键字创建出来。
RelativeLayout relativeLayout=new RelativeLayout ( this );
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams (
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT );
标题布局常用属性:
android:id
android:layout_width
android:layout_height
android:background
android:layout_margin(外边距)
android:padding(内边距)
标题布局方式
1. RelativeLayout相对布局
通过以父控件或其他子控件为参照物,指定子控件的位置
2. LinearLayout线性布局
(1)Android:orientation:
设置该布局中空间的排列顺序, vertical(竖直排列),horizontal(垂直排列)
(2)android:layout_weight:
在布局内设置控件权重,属性值可以直接写int值
当设置了button控件的android:layout_weight属性时,
控件的 android:layout_width属性值一般设置为0dp才会有权重占比的效果。
3. TableLayout表格布局
表格布局采用行和列来管理控件,它不需要声明有多少行和列,它是通过在tablelayout中添加TableRow布局或控件来控制表格行数,可以在Row布局中添加控件来控制列数。
布局常用属性:
android:stretchColumns(可被拉伸的列)
android:shrinkColumns(可被收缩的列)
android:collapseColumns(可被隐藏的列)
控件常用属性:
android:layout_column(该控件显示位置,android:layout_column="0"表示在第一个位置)
android:layout_span(该控件占据几行,默认一行)
4. FrameLayout帧布局
该布局用于在屏幕上创建一块空白区域,添加到该区域中的每个子控件占一帧,这些帧会一个一个叠加在一起,后加入的控件会叠加在上一控件上层。
属性:
android:foregroud(设置帧布局容器的前景图像【始终在所有子控件之上】)
android:foregroudGravity(设置前景图的位置)
5.ConstraintLayout约束布局
ConstraintLayout与之前介绍的相比,并不适合使用xml代码编写,但他很适合使用可视化的方式设置空间布局,可视化操作的背后也是xml实现的,只不过代码是studio自动生成的。
常用属性
layout_constraintLeft_toLeftOf
layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf