安卓开发(4)-布局管理器-xml文件

本文详细介绍了安卓开发中的几种布局管理器,包括RelativeLayout、LinearLayout、FrameLayout、TableLayout和GridLayout,讲解了它们的主要属性和用法。还提到了ConstraintLayout的使用,并探讨了布局管理器的嵌套应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

布局管理器

1.RelationLayout相对布局管理器

 

1.1 属性

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:gravity="center" 
    android:IgnoreGravity="@id/user"

 1.2 内部类

LayoutParams

1.2.1属性

 

2.LinearLayout 线性布局管理器

应用

2.1属性

android:orientation="vertical"  //组件排列方式
android:gravity="center_horizontal|bottom" 
android:layout_weight="1" // 剩余空间分配占比,用weight标识,不写为0

 

3.FrameLayout 帧布局管理器

显示层叠内容,或者可拖动内容

3.1属性

    android:foreground="@mipmap/ic_launcher" //前景,最前面显示的图片,不会被覆盖
    android:foregroundGravity="right|bottom" //前景图片位置设置

4.TableLayout表格布局管理器

4.1属性

android:collapseColumns="0,2" // 需要隐藏的列
android:stretchColumns="1" // 可以被拉伸的列
android:shrinkColumns="2" // 可以被收缩的列
  <TableRow>...</TableRow> // 一个行内含有的东西,不写就直接用组件添加一行

5.GridLayout网格布局管理器

 

5.1属性


    android:columnCount="4" // 最大列数
    android:orientation="vertical" // 默认排序方向
    android:rowCount="3" // 最大行数

        android:layout_column="0" // 位于第几列
        android:layout_row="0" // 第几行
        android:layout_columnSpan="2" // 占几列
        android:layout_rowSpan="2" // 占几行
        android:layout_columnWeight="1" // 列上权重
        android:layout_rowWeight="1" // 行上权重
        android:layout_gravity="fill" // 填充

在Java文件中,

gridLayout.getChildCount() // 子UI数量
gridLayout.getChildAt(i) // 返回子UI的View,按index来,0 ~

例如:

        Random r = new Random();
        GridLayout gridLayout = findViewById(R.id.gl);
        for (int i = 0; i < gridLayout.getChildCount(); i ++){
            TextView textView = (TextView) gridLayout.getChildAt(i);
            textView.setBackgroundColor(Color.rgb(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
        }

6.ConstraintLayout

https://www.bilibili.com/video/BV1w4411t7UQ?p=4

https://www.jianshu.com/p/17ec9bd6ca8a

7.布局管理器的嵌套

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值