目录
一、控制UI界面的方式
1.在XML布局文件中通过XML属性进行控制
//1.在Android应用的res/layout目录下,创建XML布局文件。
//2.在Activity的onCreate()方法中使用以下java代码,显示XML文件内容。
setContentView(R.layout.<资源文件名字>);
2.在Java程序代码中通过调用方法进行控制
//在onCreate()方法中
//1.创建布局管理器
LinearLayout linearLayout = new LinearLayout();
//2.创建具体的组件
Button btn = new Button();
//3.将创建的组件添加到布局管理器中
linearLayout.addView(btn);
3. XML布局文件和Java代码混合控制
二、Android常用布局管理器
布局的通用属性
1.LinearLayout 线性布局
以水平或垂直方式来显示界面中的控件。
<LinearLayout xmlns:android="http://schemas.android.com/qpk/res/android"
//布局属性列表
//排列方式
android:orientation="vertical"//垂直
android:orientation="horizontal"//水平
//对齐方式
android:gravity="center"
>
<!--控件及其属性列表-->
</LinearLayout>
2.RelativeLayout 相对布局
通过相对定位排列
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
//布局属性列表
>
<!--控件-->
<TextView android:id="@+id/view1"/>
<TextView android:id="@+id/view2"
//控件属性列表
android:layout_above="@id/view1"//view2放置在view1组件上方
/>
</RelativeLayout>
3.FrameLayout 帧布局
所有控件都默认显示在屏幕左上角,控件叠加
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
//布局属性列表
>
<!--控件-->
<TextView
//控件属性列表
android:layout_gravity="center"//组件放置在页面中间
/>
</FrameLayout>
4.TableLayout 表格布局
采用行和列的形式来管理UI组件
每次向TableLayout中添加TableRow,该TableRow就是一个表格行, 向TableRow添加一个子组件该表格就增加一列。如果直接向TableLayout中添加组件,那么该组件直接占一行。
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
//布局属性列表
>
<!--表格行-->
<TableRow>
<!--表格列-->
<!--控件及其属性列表-->
</TableRow>
</TableLayout>
5.ConstraintLayout 约束布局
可视化的方式编写界面布局