android学习笔记第一章,六大布局

LinearLayout(线性布局)

线性布局学习流程图如下:

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

RelativeLayout(相对布局)

父容器定位属性示意图

在这里插入图片描述

根据兄弟组件定位

在这里插入图片描述

图中的组件1,2就是兄弟组件了,而组件3与组件1或组件2并不是兄弟组件,所以组件3不能通过 组件1或2来进行定位,比如layout_toleftof = "组件1"这样是会报错的!切记! 关于这个兄弟组件定位的最经典例子就是"梅花布局"了,下面代码实现下:
<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"

    tools:context=".MainActivity">

    <Button
        android:id="@+id/img1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerInParent="true"
        android:text="中央"/>
    <Button
        android:id="@+id/img2"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_toLeftOf="@+id/img1"
        android:layout_centerVertical="true"
        android:text="左边"/>
    <Button
        android:id="@+id/img3"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_toRightOf="@id/img1"
        android:layout_centerVertical="true"
        android:text="右边"/>
    <Button
        android:id="@+id/img4"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_above="@+id/img1"
        android:layout_centerHorizontal="true"
        android:text="上面"/>
    <Button
        android:id="@+id/img5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/img1"
        android:text="下面"/>
</RelativeLayout>

在这里插入图片描述

margin和padding的区别

首先margin代表的是偏移,比如marginleft=“5dp”表示组件离容器左边缘编译5dp;而padding代表的则是填充,而填充的对象针对的是组件中的元素,比如TextView中的文字,比如为TextView设置paddingleft=“5dp”,则是在组件里的元素的左边填充5dp的空间!margin针对的是容器中的组件,而padding针对是组件中的元素,要区分开来!下面通过简单的代码演示两者的区别:
<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"

    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"/>
    <Button
        android:paddingLeft="100dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_toRightOf="@+id/btn1"/>
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_alignParentBottom="true"/>
    <Button
        android:layout_marginLeft="100dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_toRightOf="@+id/btn2"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

在这里插入图片描述

TableLayout(表格布局)

TableLayout的介绍

	html中<table> <tr><td>就可以生成一个html的表格 ,而Android中也允许我们使用表格的方式来排列组件,就是行与列的方式,可以直接设置多少行多少列!
	如何确定行数和列数:
	1.如果我们直接往TableLayout中添加组件的话,那么这个组件将沾满一行!!
	2.如果我们想一行上有多少个组件的话,就要添加一个TableRow的容器,把组件都丢到里面!
	3.tablerow中的组件个数就决定了该有多少行多少列,而列的宽度由该列中最宽的单元格决定
	4.tablerow的layout_width属性,默认是fill_parent的,我们自己设置成其他的值也不会生效!!但是layout_height默认是wrapten_content的,我们却可以自己设置大小!
	5.整个表格布局的宽度取决于父容器的宽度(占满父容器本身)
	6有多少行就要自己数啦,一个tablerow一行,一个单独的组件也一行!多少列则是看tableRow中的组件个数,组件最多的就是TableLayout的列数。

三个常用属性

android:collapseColumns:设置需要被隐藏的列的序号
android:shrinkColumns:设置允许被收缩的列的列序号
android:stretchColumns:设置运行被拉伸的列的列序号
以上这三个属性的列号都是0开始算的,比如shirnkColunmns=“2”,对应的是第三列!可以设置多个,用逗号隔开比如“0,2”,如果是所有列都生效,则用“*”号即可,除了这三个常用属性,还有两个属性,分别就是跳格子以及合并单元格,这和HTMLL中的Table类似:
android:layout_column=“2”:表示的就是跳过第二个,直接显示到第三个格子处,从1开始算的!
android:layout_span=“4”:表示合并4个单元格,也就是说这个组件占4个单元格

collapseColumns(隐藏列)

流程:在TableRow中定义5个按钮后,接着在最外层的TableLayout中添加以下属性:android:collapseColumns=“0,2”,就是隐藏第一于第三列,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<TableRow 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:orientation="vertical"

    tools:context=".MainActivity">
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="one"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="two"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="three"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="four"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="five"/>
    </TableRow>
</TableRow>

隐藏前
在这里插入图片描述
隐藏后
在这里插入图片描述

stretchColumns(拉伸列)

流程:在TableLayout中设置了四个按钮,接着在最外层的TableLayout中添加以下属性:android:stretchCloumns="1"设置第二列为拉伸列,让该列填满这一行所有剩余空间

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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:id="@+id/TableLayout2"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:stretchColumns="1"
    tools:context=".MainActivity">
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="one"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="two"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="three"/>

    </TableRow>
</TableLayout>

在这里插入图片描述

shrinkColumns(收缩列)

步骤:这里为了演示出效果,设置了5个按钮和一个文本框,在最外层的TableLayout中添加以下属性: android:shrinkColumns = “1”

设置第二个列为可收缩列,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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:id="@+id/TableLayout2"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:shrinkColumns="1"
    tools:context=".MainActivity">
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="one"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="two"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="three"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="four"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="five"/>
    </TableRow>
</TableLayout>

在这里插入图片描述

示例展示

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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:id="@+id/TableLayout2"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    tools:context=".MainActivity">

    <TableRow>
        <TextView/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"/>
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="150dp"/>
        <TextView/>
    </TableRow>
        <TableRow>
            <TextView/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="密码:"/>
            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minWidth="150dp"/>
            <TextView/>
        </TableRow>
    <TableRow>
        <TextView/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登录"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="退出"/>
        <TextView/>
    </TableRow>
</TableLayout>

在这里插入图片描述

FrameLayout(帧布局)

帧布局可以说是六大布局中最为简单的一个布局,这个布局直接在屏幕上开辟出一块空白的区域,当我们往里面添加控件的时候,会默认把他们放到这块区域的左上角,而这种布局方式却没有任何的定位方式,所以它应用的场景并不多;帧布局的大小由控件中最大的子控件决定,但是我们也可以通过layout_gravity属性,指定到其他的位置!

常用属性

前景图像:永远处于帧布局最上面,直接面对用户的图像,就是不会被覆盖的图片。

两个属性:
android:foreground:设置帧布局容器的前景图像
android:foregroundGravity:设置前景图像显示的位置

<?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:id="@+id/TableLayout2"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:foreground="@mipmap/ic_launcher"
    android:foregroundGravity="right|top"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#FF6143"/>
    <TextView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="#78FE00"/>
    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#FFFF00"/>

</FrameLayout>

在这里插入图片描述
代码解析:很简单,三个TextView设置不同大小与背景颜色,依次覆盖,接着右下角的是前景图像,通过android:foregrou=“”设置前景图像的图像,android:foregroundGravity=“”设置前景图像的位置在右下角

随手指移动的图像
出了点BUG,所以暂且不放代码了
gif动图,根据八张图片帧率播放
package com.example.testlinelayout;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.app.Activity;
import android.graphics.drawable.Drawable;

public class MainActivity extends AppCompatActivity {
    FrameLayout frame=null;
        Handler handler = new Handler()
        {
            int i=0;
            @Override
            public void handleMessage(Message msg)
            {
                if (msg.what == 0x123)
                {
                    i++;
                    move(i%8);
                }
                super.handleMessage(msg);
            }
        };
        void move(int i)
        {
            Drawable a = getResources().getDrawable(R.drawable.s_1);
            Drawable b = getResources().getDrawable(R.drawable.s_2);
            Drawable c = getResources().getDrawable(R.drawable.s_3);
            Drawable d = getResources().getDrawable(R.drawable.s_4);
            Drawable e = getResources().getDrawable(R.drawable.s_5);
            Drawable f = getResources().getDrawable(R.drawable.s_6);
            Drawable g = getResources().getDrawable(R.drawable.s_7);
            Drawable h = getResources().getDrawable(R.drawable.s_8);
            switch (i)
            {
                case 0:
                    frame.setForeground(a);
                    break;
                case 1:
                    frame.setForeground(b);
                    break;
                case 2:
                    frame.setForeground(c);
                    break;
                case 3:
                    frame.setForeground(d);
                    break;
                case 4:
                    frame.setForeground(e);
                    break;
                case 5:
                    frame.setForeground(f);
                    break;
                case 6:
                    frame.setForeground(g);
                    break;
                case 7:
                    frame.setForeground(h);
                    break;
            }
        }

    @Override
    protected  void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        frame = (FrameLayout) findViewById(R.id.myframe);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0x123);
            }
        },0,170);
    }
}

GridLayout(网格布局)

网格布局的优点:
可以自己设置布局中组件的排列方式
可以自定义网格布局由多少行,多少列
可以直接设置组件位于某行某列
可以设置组件横跨几行或者几列
在这里插入图片描述

简单计算器模拟

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/GridLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:orientation="horizontal"
    android:columnCount="4"
    android:rowCount="6"
    tools:context=".MainActivity">
    <TextView
        android:layout_columnSpan="4"
        android:layout_gravity="fill"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:text="0"
        android:textSize="50sp"
        android:background="#FFCCCC"/>
    <Button
        android:layout_columnSpan="2"
        android:gravity="fill"
        android:text="回退"/>
    <Button
        android:layout_columnSpan="2"
        android:gravity="fill"
        android:text="清空"/>
    <Button android:text="+"/>
    <Button android:text="1"/>
    <Button android:text="2"/>
    <Button android:text="3"/>
    <Button android:text="-"/>
    <Button android:text="4"/>
    <Button android:text="5"/>
    <Button android:text="6"/>
    <Button android:text="*"/>
    <Button android:text="7"/>
    <Button android:text="8"/>
    <Button android:text="9"/>
    <Button android:text="/"/>
    <Button
        android:layout_width="wrap_content"
        android:text="."/>
    <Button android:text="0"/>
    <Button android:text="="/>

</GridLayout>

在这里插入图片描述
代码解析:我们通过:android:layout_rowSpan与android:layout_columnSpan设置了组件横跨。多行或者多列的话,如果你要让组件填满横越过的行或列的话,需要添加下面这个属性:android:layout_gravity=“fill”。

用法归纳

1.先定义组件的对齐方式,android:roientation水平或者竖直,设置有多少行多少列
2.设置组件所在的行或列,记得是从0开始算的,不设置默认每个组件占一行一列
3.设置组件横跨几行或者几列;设置完毕后,需要在设置一个填充android:layout_gravity=”fill“

AbsoluteLayout(绝对布局)

绝对布局对机型的适配有要求,因此不是很常用,因此就不过多介绍

总结

建议使用:LinearLayout的weight权重属性+RelativeLayout来构建我们的界面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值