一、常用布局
线性布局(Linear layout)
相对布局(Relative layout)
帧布局(Frame layout)
表格布局(Table layout)
网格布局(Grid layout)
约束布局(Constrained layout)
二、添加布局
1.利用XML文件设计
把写好的xml文件用R.layout引用出来
2.利用java代码添加
因为setContentView方法还有可以调用视图(view)的
所以:
//1.根布局为线性布局
LinearLayout fff=new LinearLayout(this);
//2.设置宽高,match铺满,第一个参数代表宽度,第二个参数代表高度
fff.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
//3.背景设置为红色
fff.setBackgroundColor(Color.RED);
//4.指定此Activity的内容视图(view)为该线性布局
setContentView(fff);
如果要继续+控件,使用
三、布局重要属性
1. android:layout_width=" " 宽度
- match_parent 匹配|适配父元素窗口大小
- wrap_content 实际大小随着内容的变化而改变
- 实际的数值 60dp/80sp
android:layout_width="wrap_content"
android:layout_width="match_parent"
2. android:layout_height=" " 高度
android:layout_height="wrap_content"
android:layout_height="match_parent"
3. android:padding=" " 内边距
android:padding="20dp"
4.android:layout_margin=" " 外边距
android:layout_margin="20dp"
四、线性布局(Linear layout)
4.0线性布局重要属性
1. android:orientation=" " 方向
<!-- vertical:垂直的 horizontal:水平的-->
android:orientation="vertical"
android:orientation="horizontal"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_weight=" "
android:layout_gravity="center_horizontal">
<!-- vertical:垂直的 horizontal:水平的-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_margin="20dp"
android:padding="20dp"
android:background="#ED380B" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_margin="20dp"
android:padding="20dp"
android:background="#18FDE3" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_margin="20dp"
android:padding="20dp"
android:background="#84F852" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_margin="20dp"
android:padding="20dp"
android:background="#F8EF52" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_margin="20dp"
android:padding="20dp"
android:background="#B229FD" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="horizontal"
>
<!-- vertical:垂直的 horizontal:水平的-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="15sp"
android:background="#ED380B"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="15sp"
android:background="#2197F1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="15sp"
android:background="#23F186" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="15sp"
android:background="#951BFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="15sp"
android:background="#FBE346" />
</LinearLayout>
2.android:layout_weight=" "权重
android:layout_weight="1"
放在控件中,
显示图示效果
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="horizontal"
>
<!-- vertical:垂直的 horizontal:水平的-->
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="你好"
android:layout_weight="1"
android:textSize="20sp"
android:background="#ED380B"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="你好"
android:layout_weight="2"
android:textSize="20sp"
android:background="#2197F1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="你好"
android:layout_weight="1"
android:textSize="20sp"
android:background="#23F186" />
</LinearLayout>
特别注意
水平布局时,宽度设置为0dp
垂直布局时,高度设置为0dp
否则
相反,改成0dp的话,就只会固定比例
3.android:layout_gravity=" " 重力
【另】android:gravity=" "是控制控件里的内容