Android七大布局

七大布局分别为:线性布局(LInearLayout)、相对布局(RelativeLayout)、帧布局(FrameLayout)、表格布局(TableLayout)、绝对布局(absoluteLayout)、网格布局(GridLayout)、约束布局(ConstraintLayout)

一  线性布局LinearLayout

顾名思义,指的是整个Android布局中的控件摆放方式是以线性的方式摆放的。

属性 android:orientation = “vertical” | “horizontal” 竖直或水平,默认水平

android:orientation控制方向,属性值垂直(vertical)和水平(horizontal),默认水平方向。


属性 android:gravity = “top”|"center”|“bottom” 相对于父容器的对齐方式

android:gravity:内部控件对齐方式,常用属性值有center、center_vertical、center_horizontal、top、bottom、left、right等。
这个属性在布局组件RelativeLayout、TableLayout中也有使用,FrameLayout、AbsoluteLayout则没有这个属性。
center:居中显示,这里并不是表示显示在LinearLayout的中心,当LinearLayout线性方向为垂直方向时,center表示水平居中,但是并不能垂直居中,此时等同于center_horizontal的作用;同样当线性方向为水平方向时,center表示垂直居中,等同于center_vertical。
top、bottom、left、right顾名思义为内部控件居顶、低、左、右布局。

属性 android:layout_gravity = “top” | “center” | “bottom” 内部的布局方式

layout_gravity是用来设置自身相对于父元素的布局。


属性 android:layout_weidht 使用比例方式执行控件的大小,在手机屏幕适配方面起到非常重要的作用
android:layout_weight:权重,用来分配当前控件在剩余空间的大小。
使用权重一般要把分配该权重方向的长度设置为零,比如在水平方向分配权重,就把width设置为零。

1. 排列方式

  • 纵向:android:orientation="vertical"
  • 横向:android:orientation="horizontal"
    系统默认采用横向布局

代码只需要修改android:orientation="horizontal"即可。下面为对比代码和对比图:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal"> 
    <!--横向布局-->

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/colorAccent"/>
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/colorPrimary"/>

</LinearLayout>

2. 对齐方式

线性布局里有两种设置边距的方式,分别是padding()margin()。前者规定内边距,后者规定外边距。

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <!--横向布局-->

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/colorPrimary"/>

    <TextView
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:background="@color/colorAccent"
        android:paddingStart="150dp"  //和左侧的边距
        android:paddingTop="50dp"   //和顶部的边距
        android:text="这是一个子控件"

        ></TextView>
    
</LinearLayout>

 


黑色箭头为margin(),白色箭头为padding(),可以看到文字可以和其背景对齐,这一整个文本控件又和界面对齐。线性布局中,各个控件不能重叠

 

3. 权重

线性布局中可以规定控件的权重,通过android:layout_weight=""实现。下面我们来看一起权重的经典问题。我们先不设置总权重,设置子元素的宽度为0dp。

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal">
    <!--横向布局-->


    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"//果绿色
        android:layout_weight="1"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"//玫红色
        android:layout_weight="2"/>

</LinearLayout>

可以看到界面控件的比例与权重相符,再看一下权重超过设定的情况。下面规定总权重为3,子元素宽度都设置为0dp,看看会发生什么?

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal"
    android:weightSum="3"> // 规定总权重为3


    <ImageView
        android:layout_width="0dp" // 设置宽度
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        android:layout_weight="1"/>

    <ImageView
        android:layout_width="0dp" // 设置宽度
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        android:layout_weight="2"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark"
        android:layout_weight="3"/>
    
</LinearLayout>

 


明明我们设置了1:2:3,为什么显示的却是1:2呢?这是因为系统中规定,子权重不能大于总权重,如果大于了,则“先到先得”。这里我们用了横向排列方式,故第一个控件(果绿色)实际权重为1 * (1+2+3) = 1/6,第二个控件(玫红色)的实际权重为2 * (1+2+3) = 2/6超过总权重的控件将不被显示,那么显示的比例就是(1/6) : (2/6)1 : 2

 

注意:使用权重时,尽量将控件宽度设置为0dp,否则其比例并不会按其权重显示,那么如何显示呢?这里有一篇文章我觉得讲得挺好的,可以直接看其文字部分。戳我查看

 

 

 

下面来看看代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    an
  • 4
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值