【Android入门到项目实战--3.2】—— 详解4种基本布局(线性布局、相对布局、帧布局、百分比布局)

目录

一、线性布局

1、  android:layout_gravity属性

2、android:layout_weight属性

二、相对布局

如何相对于控件定位?

三、帧布局

四、百分比布局


本篇文章主要讲解四种基本的布局:线性布局、相对布局、帧布局、百分比布局。

一、线性布局

        LinearLayout又称线性布局,是非常常用的布局,此布局将它所包含的控件在线性方向上依次排列,其android:orientation属性指明了排列方向,如果是vertical,则为竖直排列,如果是horizontal,则是水平排列。

下面尝试演示。 

XML布局文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3"/>

</LinearLayout>

 效果如下:

 

改变orientation属性为horizontal后效果如下:

 

1、  android:layout_gravity属性

下面加入新属性尝试一下。

XML布局文件代码如下:

         android:layout_gravity用于指定控件在布局中的对齐方式,而android:gravity指定文字在控件中的对齐方式。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:text="按钮1"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="按钮2"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="按钮3"/>

</LinearLayout>

 效果如下:

 

2、android:layout_weight属性

         这个属性允许我们使用比例的方式来指定控件大小,在手机屏幕的适配性方面起到重要作用。

下面尝试实现:编写一个消息发送界面。

XML布局文件代码如下:  

        宽度属性都设置为0dp,因为宽度不再由android:layout_width指定;那么android:layout_weight属性该如何设置?系统会把LinearLayout下所有控件指定的layout_weight值相加,得到总值,然后每个控件所占的大小比例就是该控件的weight值除以刚才的总值,这里都为1,各占1/2比例。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <EditText
        android:id="@+id/input_message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="请输入"/>
    
    <Button
        android:id="@+id/send"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="发送"/>

</LinearLayout>

 效果如下:

 

 

二、相对布局

        RelativeLayout又称相对布局,也是非常常用的布局,此布局可通过相对定位的方式让控件出现在布局的任何位置

下面尝试演示一下。

XML布局文件代码如下:  

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <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="按钮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="按钮2"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="按钮3"/>

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="按钮4"/>

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="按钮5"/>


</RelativeLayout>

 效果如下:

 

 以上相对于父布局定位。

如何相对于控件定位?

XML布局文件代码如下:   

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_toLeftOf="@+id/button3"
        android:text="按钮1"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_toRightOf="@id/button3"
        android:text="按钮2"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="按钮3"/>

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button3"
        android:layout_toLeftOf="@+id/button3"
        android:text="按钮4"/>

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button3"
        android:layout_toRightOf="@+id/button3"
        android:text="按钮5"/>


</RelativeLayout>

 效果如下:

 

三、帧布局

        FrameLayout又称帧布局,此布局下所有控件默认摆放在布局的左上角。

XML布局文件代码如下: 

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

      <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是TextView"/>

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>


</FrameLayout>

 效果如下: 

 

四、百分比布局

        只有LinearLayout支持使用layout_weight属性实现按比例大小指定控件,而其他两个不支持,Android引入百分比布局解决此问题。

        此布局不同于前面的布局,属于新增布局,如何让新增布局在所有版本中能够使用?百分比布局在support库中,只需要在build.gradle中添加百分比布局库的依赖,就能保证兼容性。

此布局后继文章实现。


希望本文章对你有帮助,如果你对Android开发感兴趣,请持续关注本专栏,帮助你从入门到项目实战,你将收获:Android基础开发、各种经典功能实现、项目实战、开发自己的APP、将APP上传应用商店、靠广告赚钱等等,持续更新ing......

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,Android系统中常用的五布局方式分别是: 1. 线性布局(LinearLayout):线性布局是一非常常见的布局方式,它按照水平或者垂直方向排列控件,每个控件可以通过权重属性来占据剩余空间。线性布局简单易用,但是不能灵活地适应不同的屏幕大小和分辨率。 2. 相对布局(RelativeLayout):相对布局是根据控件之间的相对位置来排列控件的,它可以根据需要调整控件的位置和大小,适应不同的屏幕大小和分辨率。但是相对布局的层级较多时,性能会受到影响。 3. 布局(FrameLayout):布局是将控件按照层叠顺序排列在一起的布局方式,每个控件占据整个布局的全部空间。布局适用于需要在同一个位置上显示多个控件的情况,也可以用来实现简单的动画效果。 4. 表格布局(TableLayout):表格布局是将控件按照行列的方式排列在一起的布局方式,每个控件占据一个单元格。表格布局适用于需要显示大量数据的情况,但是在屏幕较小的设备上可能会导致单元格过小而无法显示完整。 5. 网格布局(GridLayout):网格布局是将控件按照网格的方式排列在一起的布局方式,每个控件占据一个单元格,可以通过设置权重属性来占据多个单元格。网格布局适用于需要显示复杂结构的情况,但是在屏幕较小的设备上可能会导致单元格过小而无法显示完整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四月天行健

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值