Android studio的常见布局

什么是布局?

就是把界面中的控件按照某种规律摆放到指定的位置

布局的二种实现

代码
xml配置文件:res/layout目录下
注:也可以同时使用xml和代码

目录

[TOC]来生成目录:

padding 内补丁
margin 外补丁
background 颜色
match_parent/fill_parent 匹配父类的内容
wrap_content 适应自己的内容

LinearLayout 线性布局

orientation=”vertical(垂直) | horizontal(平行)”

android:gravity和android:layout_gravity的区别

android:gravity:控件内部的元素(对内有效)
android:layout_gravity:控件所在父元素的位置(对外生效)

例如:

<?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">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"/>

</LinearLayout>

FrameLayout 帧布局

注:帧布局有点类似于awt的CardLayout都是把组件一个一个叠加到一起,
但CardLayout能将下面的组件移上来,但FrameLayout没有提供此功能

例如:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

   <ImageView
        android:layout_width="200dp"
        android:background="@color/aqua"
        android:layout_gravity="center"
        android:layout_height="200dp" />

    <ImageView
        android:layout_width="150dp"
        android:background="@color/crimson"
        android:layout_gravity="center"
        android:layout_height="150dp" />

    <ImageView
        android:layout_width="120dp"
        android:background="@color/lawngreen"
        android:layout_gravity="center"
        android:layout_height="120dp" />

    <ImageView
        android:layout_width="80dp"
        android:background="@color/springgreen"
        android:layout_gravity="center"
        android:layout_height="80dp" />

    <ImageView
        android:layout_width="60dp"
        android:background="@color/fuchsia"
        android:layout_gravity="center"
        android:layout_height="60dp" />

</FrameLayout>

RelativeLayout 相对布局

注1:注意XML中组件的顺序,不然会报错
注2:android新版本中组件的定义顺序没有关系

RelativeLayout支持的二个xml属性

android:gravity :设置该布局容器内各子组件的对齐方式
android:ignoreGravity:设置哪个组件不受gravity属性的影响

控制子组件布局的内部类RalativeLayout.LayoutParams 此内部类的属性分二类
1、 boolean
    相对父元素
    alignParent...
    center...(只有在父元素中才存在水平或垂直居中等)
2、 id型
    @+id和@id的区别
    @+id/x1(添加新ID)
    @id/x1(引用此ID)
    相对于指定元素(根据ID指定)
layout_toRightOf,layout_toLeftOf(是一种靠拢动作)

例如:


<RelativeLayout 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">

    <Button
    android:layout_width="60dp"
    android:background="@color/red"
    android:layout_height="60dp" />

    <Button
    android:layout_width="60dp"
    android:background="@color/coral"
    android:layout_centerHorizontal="true"
    android:layout_height="60dp" />

    <Button
    android:layout_width="60dp"
    android:background="@color/yellow"
    android:layout_alignParentRight="true"
    android:layout_height="60dp" />

    <Button
    android:layout_width="60dp"
    android:background="@color/blue"
    android:layout_centerInParent="true"
    android:id="@+id/main_btn_blue"
    android:layout_height="60dp" />

    <Button
    android:layout_width="60dp"
    android:background="@color/green"
    android:layout_toLeftOf="@id/main_btn_blue"
    android:layout_centerInParent="true"
    android:layout_marginRight="20dp"
    android:layout_height="60dp" />

    <Button
    android:layout_width="60dp"
    android:background="@color/darkblue"
    android:layout_toRightOf="@id/main_btn_blue"
    android:layout_centerInParent="true"
    android:layout_marginLeft="20dp"
    android:layout_height="60dp" />


    <Button
    android:layout_width="match_parent"
    android:background="@color/darkviolet"
    android:layout_alignParentBottom="true"
    android:layout_height="60dp" />

</RelativeLayout>

GridLayout 网格布局

常用属性

布局相关属性:rowCount、columnCount
子控件相关属性:layout_gravity=”fill_horizontal|fill_vertical”

例如:

<GridLayout 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"
    android:rowCount="5"
    android:columnCount="4"
    tools:context=".MainActivity">

   <Button
        android:layout_width="60dp"
        android:background="@color/red"
        android:text="1"
        android:layout_height="60dp" />

    <Button
        android:layout_width="60dp"
        android:background="@color/coral"
        android:layout_centerHorizontal="true"
        android:text="2"
        android:layout_height="60dp" />

    <Button
        android:layout_width="60dp"
        android:background="@color/yellow"
        android:text="3"
        android:layout_alignParentRight="true"
        android:layout_height="60dp" />

    <Button
        android:layout_width="60dp"
        android:background="@color/blue"
        android:text="5"
        android:layout_centerInParent="true"
        android:id="@+id/main_btn_blue"
        android:layout_rowSpan="2"
        android:layout_gravity="fill_vertical"
        android:layout_height="60dp" />

    <Button
        android:layout_width="60dp"
        android:background="@color/green"
        android:text="4"
        android:layout_toLeftOf="@id/main_btn_blue"
        android:layout_centerInParent="true"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"
        android:layout_height="60dp" />

    <Button
        android:layout_width="60dp"
        android:background="@color/darkblue"
        android:layout_toRightOf="@id/main_btn_blue"
        android:layout_centerInParent="true"
        android:text="6"
        android:layout_height="60dp" />

    <Space  />


</GridLayout>

注:Space标签的作用:挡住控件,让其不超出网格的范围

猜你喜欢

转载自blog.csdn.net/qq_41454799/article/details/82595203
今日推荐