当前位置: 移动技术网 > 移动技术>移动开发>Android > android 常用布局文件(LinearLayout,RelativeLayout,FrameLayout,ConstraintLayout,TableLayout)

android 常用布局文件(LinearLayout,RelativeLayout,FrameLayout,ConstraintLayout,TableLayout)

2020年07月18日  | 移动技术网移动技术  | 我要评论

常用布局

LinearLayout-线性布局:

LinearLayout-线性布局有两个方向:水平和垂直方向。分别是通过android:orientation="horizontal"和android:orientation="vertical"来控制的
在这里插入图片描述
1.常用属性

orientation:布局中组件的排列方式,有horziontal(水平)和vertical(垂直)两种方式。

gravity:控制组件所包含的子元素的对齐方式,可多个组合,如(left|buttom)。

layout_gravity:控制该组件在父容器里的对齐方式。

layout_width:布局的宽度,通常不直接写数字,用wrap_content(组件实际大小),fill_parent或者match_parent填满父容器。

layout_height:布局的高度,参数同上。

id:为该组件设置一个资源id,在java文件中可以通过findViewByid(id)找到该组件。

background:为该组件设置一个背景图片,或者直接用颜色覆盖。
layout_weight:权重属性。这个属性的意思是分配剩余空间。

比如有俩个控件,分别设置为android:layout_weight=“1”,android:layout_weight=“2”,表示控件分别占屏幕的1/3和2/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"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <!-- android:orientation="vertical" 一定要写到头文件中,不然会出错-->
    <!--第一个例子是垂直布局,第二个例子是横向布局-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#120ff0" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#950ff0" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#079ff0" />
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#000fca" />
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#000f65" />
    </LinearLayout>
</LinearLayout >
RelativeLayout-相对布局

相对,顾名思义是有参照的,就是以某个兄弟组件,或者父容器来决定的(兄弟组件是在一个同一个布局里面的组件,如果是布局里一个组件参照另一个布局里的组件会出错)
1.基本属性

gravity:设置容器内组件的对齐方式。

ignoreGravity:设置了该属性为true的属性的组件,将不受gravity属性的影响。

2.根据父容器定位

layout_alignParentLeft:左对齐。

layout_alignParentRight:右对齐。

layout_alignParentTop:顶部对齐。

layout_alignParentBottom:底部对齐。

layout_centerHorizontal:水平居中。

layout_centerVertical:垂直居中。

layout_centerInParent:中间位置。

3.根据兄弟组件定位

layout_toLeftOf:参考组件的左边。

layout_toRightOf:参考组件的右边。

layout_above:参考组件的上方。

layout_below:参考组件的下方。

layout_alignTop:对齐参考组件的上边界。

layout_alignBottom:对齐参考组件的下边界。

layout_alignLeft:对齐参考组件的左边界。

layout_alignRight:对齐参考组件的右边界。

4.margin(偏移)

layout_margin:设置组件上下左右的偏移量。

layout_marginLeft:设置组件离左边的偏移量。

layout_marginRight:设置组件离右边的偏移量。

layout_marginTop:设置组件离上面的偏移量。

layout_marginBottom:设置组件离下面的偏移量。

5.padding(填充)

padding:往内部元素的上下左右填充一定边距。

paddingLeft:往内部元素的左边填充一定边距。

paddingRight:往内部元素的右边填充一定边距。

paddingTop:往内部元素的上方填充一定边距

paddingBottom:往内部元素的下方填充一定边距。

FrameLayout-帧布局

设计FrameLayout是为了显示单一项widget。通常,不建议使用FrameLayout显示多项内容;因为它们的布局很难调节。不用layout_gravity属性的话,多项内容会重叠;使用layout_gravity的话,能设置不同的位置。layout_gravity可以使用如下取值:

top :将对象放在其容器的顶部,不改变其大小.

bottom:将对象放在其容器的底部,不改变其大小.

left:将对象放在其容器的左侧,不改变其大小.

right:将对象放在其容器的右侧,不改变其大小.

center_vertical:将对象纵向居中,不改变其大小.

垂直对齐方式:垂直方向上居中对齐。

fill_vertical:必要的时候增加对象的纵向大小,以完全充满其容器.

垂直方向填充

center_horizontal:将对象横向居中,不改变其大小.

水平对齐方式:水平方向上居中对齐

fill_horizontal:必要的时候增加对象的横向大小,以完全充满其容器.

水平方向填充:center

将对象横纵居中,不改变其大小.

fill:必要的时候增加对象的横纵向大小,以完全充满其容器.

clip_vertical:附加选项,用于按照容器的边来剪切对象的顶部和/或底部的内容. 剪切基于其纵向对齐设置:顶部对齐时,剪切底部;底部对齐时剪切顶部;除此之外剪切顶部和底部.

垂直方向裁剪

clip_horizontal

附加选项,用于按照容器的边来剪切对象的左侧和/或右侧的内容. 剪切基于其横向对齐设置:左侧对齐时,剪切右侧;右侧对齐时剪切左侧;除此之外剪切左侧和右侧.

水平方向裁剪

注意: 区分“android:gravity”和“android:layout_gravity”。

android:gravity :是对控件本身来说的,是用来设置“控件自身的内容”应该显示在“控件自身体积”的什么位置,默认值是左侧。

android:layout_gravity:是相对于控件的父元素来说的,设置该控件在它的父元素的什么位置

ConstraintLayout(约束布局)

ConstraintLayout 能够灵活地定位和调整子View的大小,子 View 依靠约束关系来确定位置。

功能:
相对定位 (横向:Left、Right、Start、End ;纵向:Top、Bottom、Baseline(文本底部的基准线))
外边距
居中和倾向
可见性的表现
尺寸约束
Chain
辅助工具

常用属性:
layout_constraintLeft_toLeftOf
layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf

在这里插入图片描述
在RelativeLayout中,把控件放在布局中间的方法是把layout_centerInParent设为true,而在ConstraintLayout中的写法是

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"

RelativeLayout中的水平居中layout_centerHorizontal相当于在ConstraintLayout约束控件的左右为parent的左右(app:layout_constraintRight_toRightOf=“parent”, app:layout_constraintTop_toTopOf=“parent”);

RelativeLayout中的垂直居中layout_centerVertical相当于在ConstraintLayout约束控件的上下为parent的上(app:layout_constraintTop_toTopOf=“parent”,app:layout_constraintBottom_toBottomOf=“parent”)

bias属性,表示子控件相对父控件的位置倾向,可以使用属性:
假设设置控件A相对父控件横向偏差是30%:
在这里插入图片描述
<android.support.constraint.ConstraintLayout …>
<Button android:id="@+id/button" …
app:layout_constraintHorizontal_bias=“0.3”
app:layout_constraintLeft_toLeftOf=“parent”
app:layout_constraintRight_toRightOf=“parent”/>
</android.support.constraint.ConstraintLayout>

弧形定位(Circular positioning)
在这里插入图片描述
layout_constraintCircle : 相对控件的id
layout_constraintCircleRadius : 相对控件中心的距离,也就是圆的半径
layout_constraintCircleAngle : 相对夹角 (从 0 ~ 360度)
例如,上图示例

<Button android:id="@+id/buttonA" … />
<Button android:id="@+id/buttonB" …
app:layout_constraintCircle="@+id/buttonA"
app:layout_constraintCircleRadius=“100dp”
app:layout_constraintCircleAngle=“45” />

一般情况下,设置 GONE属性后,控件就不会出现在布局中了,B控件对A控件的margin属性也就没有作用了。

但是 ConstraintLayout 能对已经设置 GONE属性的控件进行特殊处理。当A控件设置 GONE之后,A控件相当于变成了一个点,B控件相对于对A的约束仍然是起作用的
goneMargin主要用于约束的控件可见性被设置为gone的时候使用的margin值,属性如下:
layout_goneMarginStart
layout_goneMarginEnd
layout_goneMarginLeft
layout_goneMarginTop
layout_goneMarginRight
layout_goneMarginBottom

表格布局(TableLayout)

(1)需要和TableRow标记配合使用,TableRow的数量决定表格的行数。

(2)表格的列数是由包含最多控件的TableRow决定的。

(3)TableRow的layout_width属性默认为match_parent,其他值不会生效。

(4)layout_height默认是wrap_content,可以设置大小。

(5)整个表格布局的宽度和取决于父容器的宽度。

2.常用属性

collapseColumns:设置需要被隐藏的列的序号。

shrinkColumns:设置允许被收缩的列的序列号。

stretchColumns:设置允许被拉伸的列的序列号。

注意:列号从0开始。

layout_column=“2”跳过第二个格子,直接显示到第三个格子处。

layout_span="4"合并四个单元格

本文地址:https://blog.csdn.net/qq_32324617/article/details/107339344

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网