Android学习笔记二:编写布局方式和常见布局

界面布局编写包括在XML文件中编写布局(层次更清晰)和在Java代码中编写布局。

android:layout_centerInparent="true"//XML实现控件放到布局中间

layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);//Java实现控件放到布局中间

RelativeLayout relativeLayout=new RelativeLayout(this);//创建线性布局

TextView textView=new TextView(this);
textView.setText("文本内容");
textView.setTextColor(Color.文本颜色);

setContentView(页面名)//显示到布局中

常见布局一:RelativeLayout相对布局

通过相对定位的方式指定子空间位置,即以其他控件或父容器为参照物,摆放控件位置。

//定义格式
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
属性="属性值"
...
>
</RelativeLayout>

常见布局二:LinearLayout线性布局

主要以水平或垂直方式来显示界面中的控件,当控件水平排列时,显示顺序依次为从左到右,当控件垂直排列时,显示顺序依次为从上到下。

//定义格式
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal/vertical"
属性="属性值"
...
>//orientation设置线性布局内控件排列顺序的属性
</LinearLayout>

layout_weight//权重属性设置

常见布局三:TableLayout表格布局

采用行、列的形式来管理控件,不需要声明行列数,通过在TableLayout布局添加TableRow布局来控制表格行数,在TableRow布局中添加控件来控制表格的列数。

//定义格式
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
属性="属性值"
...
>
<TableRow>
UI控件
</TableRow>
...
</TableLayout>

//表格布局属性
android:stretchColumns//设置该属性被拉伸
android:shrinkColumns//设置该属性被收缩
android:collapseColumns//设置该列被隐藏

//表格布局控件属性
android:layout_column//设置该单元显示位置
android:layout_span//设置该单元格占据几行,默认为1行。

常见布局四:FrameLayout帧布局

用于屏幕上创建一块空白区域,添加到该区域中的每个子控件占一帧,这些帧会一个一个叠加在一起,后加入的控件会叠加在上一个控件上层显示。

  • 所有控件都默认显示在屏幕左上角
//定义格式
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:foreground=""
android:foregroundGravity=""
属性="属性值"
...
>//android:foreground=""设置前景图像,前景图像始终在最上方
//android:foregroundGravity=""设置前景图像显示位置
</FrameLayout>

常见布局五:ConstraintLayout约束布局

适用于可视化方式编写界面布局,表面是Android studio根据我们操作自动生成,实际仍为XML代码实现。
新属性:

  • 相对定位:一个控件相对另一个控件进行定位。
  • 居中定位和倾向
  • Chain

猜你喜欢

转载自blog.csdn.net/jinyeran/article/details/118344185
今日推荐