一、线性布局
LinearLayout 又称线性布局,这个布局会将它所包含的控件在线性方向上一次排列。水平横向排列和垂直纵向排列。通过android:orientation属性来指定。vertical表示垂直方向,horizontal表示水平横向。修改activity_main.xml中的代码。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:baselineAligend="false"
android:divider="2"
android:measureWithLargestChild="true"
android:layout_width="100dp"
android:layout_height="wrap_content">
再添加几个按钮Button就可以看出LinearLayout布局的效果了。android:baselinetation该属性用来管理该布局管理器与其他的子元素的基线对其方式。android:divider该属性用来管理垂直布局时两个按钮的分隔条。android:measureWithLargestChild 该属性用来管理所有带权重的子元素具有最大子元素的尺寸。
二、相对布局
RelativeLayout又称作相对布局。和LinearLayout的排列规则不同的是,RelativeLayout显得更加随意一些。它可以通过相对定位的方式让控件出现在布局的任何位置。修改activity_main.xml中的代码。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button 2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button 3" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="Button 4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="Button 5" />
</RelativeLayout>
android:padding属性指定了控件的四周的内部留出的边距,当然还有上下左右。android:layout_alignParentLeft、android:layout_alignParentRight、android:layout_alignParentBottom、android:layout_alignParentTop、android:layout_centerInParent用于指定位置,上中下左右。还可以根据控件相对于控件定位。假如说相对于Button 3进行定位,则把Button 1、Button 2、Button 4、Button 5、的定位控件依次改为。
android:layout_above="@id/button3"
android:layout_toLeftOf="@id/button3"
android:layout_above="@id/button3"
android:layout_toRightOf="@id/button3"
android:layout_below="@id/button3"
android:layout_toLeftOf="@id/button3"
android:layout_below="@id/button3"
android:layout_toRightOf="@id/button3"
三、帧布局
FrameLayout又称作帧布局,这种布局没有方便的定位方式,所有的控件都是默认地摆放在布局的左上角。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="20dp"
android:foregroundGravity="right"
android:foreground:属性用于设置帧布局的前景图像。android:foregroundGravity属性用于设置前景图像的位置。除了默认的效果还可以使用android:gravity属性来指定控件在布局中的对齐方式。
四、百分比布局
百分比布局可以不再使用wrap_content和match_parent等方式来指定控件的大小,而是直接指定控件在布局中的百分比,这样就可以轻松实现平分布局和任意比例分割布局了。百分比布局属于新增的布局,Android团队将它定义在了support库里面,我们要使用它就得在项目build.gradle中添加百分比布局库的依赖,就能保证在所有Android系统版本上的兼容性了。打开app/buile.gradle文件,在dependencies闭包中添加如下内容。
dependencies {
compile fileTree(dir:'libs',include:['*.jar'])
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:percent:26.1.0'
testCompile 'junit:4.12'
}
注意:每当修改了gradle里面的文件时,Android Studio 都会弹出一个提示。
这个提示告诉我们,gradle文件自上次同步之后又发生了变化,需要再次同步才能使项目正常工作。此时需要点击Sync Now,然后gradle会开始同步,把我们新添加的百分比布局库引入到项目中。接下来修改activity_main.xml中的代码
<android.support.percent.PercentFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:text="Button 1"
android:layout_gravity="right|top"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"/>
<Button
android:id="@+id/button2"
android:text="Button 2"
android:layout_gravity="left|bottom"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"/>
<Button
android:id="@+id/button3"
android:text="Button 3"
android:layout_gravity="right|bottom"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"/>
<Button
android:id="@+id/button4"
android:text="Button 4"
android:layout_gravity="left|top"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"/>
</android.support.percent.PercentFrameLayout>
五、自定义布局
MyLayout又称自定义布局。自定义布局主要重写两个方法:onMeasure()这个是自定义容器的大小,onLayout()这个是写子元素的布局。布局一共有三种计算模式。MeasureSpec.EXACTLY:精确尺寸,MeasuroSpec.AT_MOST:最大尺寸。MeasureSpec.UNSPECIFIED:未指定尺寸。
六、表格布局
TableLayout 又称表格布局。类似于以行和列的形式对控件进行管理,每一行为一个TableRow对象,或一个View控件。当是TableRow对象时,可在TableRow下添加子控件,默认情况下,每一个子控件都各占据一列。当为View时:该View将独占一行。
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:collapseColumns="4"
android:shrinkColumns="2"
android:stretchColumns="2"
</TableLayout>
android:collapseColumns设置第几列为隐藏。android:shrinkColumns属性设置第几列为收缩列。android:stretchColumns属性设置第几列为伸展列。