布局<LinearLayout \RelativeLayout \FrameLayout \ConstraintLayout>

目录

LinearLayout

常用属性

RelativeLayout

常用属性

基于父空间,取值:true/false

基于其他控件,取值:控件ID

与其他控件对齐

FrameLayout

同LinearLayout可以通过layout_gravity指定元素位置

ConstraintLayout



LinearLayout


线性布局是比较常用的布局,它可以让控件在水平或垂直方向排列并指定排列方式,比如让控件在水平方向居中。

<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="button1"/>
</LinearLayout>


常用属性


orientation用来指定布局方向。
vertical //垂直方向
horizontal //为水平方向
layout_gravity用来指定控件的排列方式。需要注意的是,当orientation为vertical时,只有水平方向的取值才会起作用;同样当orientation为horizontal时,只有垂直方向的取值才会起作用。
left    //居左
right   //居右
center_horizontal    //水平居中
start    //当屏幕方向是 从左到右 时,同left
end    //当屏幕方向是 从左到右 时,同right
top   //居上
bottom    //居下
center_vertical    //垂直居中
center    //同center_horizontal | center_vertical
layout_weight指定控件占比

<?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">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="send"/>
    </LinearLayout>
</LinearLayout>


RelativeLayout


相对布局,也是比较常用的布局,可以设置控件相对与父控件的位置,以及设置相对于其他控件的位置。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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_centerInParent="true"
        android:text="button1"/>
</RelativeLayout>


常用属性


基于父空间,取值:true/false


layout_alignParentTop    //顶部
layout_alignParentBottom
layout_alignParentLeft
layout_alignParentRight
layout_centerInParent
layout_alignParentStart
layout_alignParentEnd


基于其他控件,取值:控件ID


layout_above
layout_toLeftOf
layout_toRightOf
layout_below
layout_toStartOf
layout_toEndOf


与其他控件对齐


layout_alignTop
layout_alignBottom
layout_alignLeft
layout_alignRight
设置button1居中显示,设置button2和button1左边对齐

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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_centerInParent="true"
        android:text="button1"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/button1"
        android:text="button\2"/>
</RelativeLayout>



FrameLayout


帧布局,比较少用,类似Flutter的Stack布局,如果没有指定位置,多个元素会堆叠到左上角。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello world"/>
</FrameLayout>


同LinearLayout可以通过layout_gravity指定元素位置



ConstraintLayout


Android Stuido支持以可视化方式编辑界面,但是效果不太好,因此引入了ConstraintLayout,它非常适合以可视化方式编辑。另外,ConstraintLayout可以有效地解决布局嵌套过多的问题。我们平时编写界面,复杂的布局总会伴随着多层的嵌套,而嵌套越多,程序的性能也就越差。ConstraintLayout则是使用约束的方式来指定各个控件的位置和关系的,它有点类似于RelativeLayout,但远比RelativeLayout要更强大。
使用约束布局首先要引入依赖
dependencies {
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
}
Palette窗口放置的是控件,我们可以拖动这些控件到布局中。Attributes窗口中是控件的属性,在这里可以设置控件的属性。
中间的主操作区有两个类似手机屏幕的界面,左边白色的为预览界面,右边为蓝图界面,可以在预览界面预览最终效果,在蓝图节目调整控件约束。
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值