android 布局2(基本布局)

这篇博客详细介绍了Android中的LinearLayout和RelativeLayout两种布局。LinearLayout通过android:orientation属性控制元素的排列方式,android:layout_weight属性用于分配剩余空间。而RelativeLayout则允许元素相对定位,提供了丰富的布局选项,如layout_alignParentLeft、layout_toRightOf等。

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

//线性布局(LinearLayout)

也就是最开始的布局

当按钮超过屏幕(这一行)的时候  就自动会显示到第二行 

有几个比较重要的属性

 android:orientation="horizontal"   显示在一行

 android:orientation="vertical"   显示在列(竖着d的行) 

android:layout_weight:权重,用来分配当前控件在剩余空间的大小。
使用权重一般要把分配该权重方向的长度设置为零,比如在水平方向分配权重,就把width设置为零。

比如说  a控件里设置 android:layout_weight=1

            b控件里也设置android:layout_weight=1  

那么结果会是2/1   各自占用屏幕的一半 

如果这个布局文件里android:orientation="horizontal" 

那么结果会是

如果这个布局文件里android:orientation="vertical" 

那么结果会是横着切割

这就是两种不同的排序方式

android:layout_weight 也就是分割屏幕剩余空间 

如果说  a控件里设置 android:layout_weight=1

            b控件里也设置android:layout_weight=2  

            c控件里也设置android:layout_weight=2  

那么得到的结果是4/2  4/1 4/1  这样分割的(主要在于屏幕剩余空间是什么)

这一点请转移到另外一篇博客看  

链接 :https://blog.youkuaiyun.com/yjt520557/article/details/83097431

//绝对布局  AbsoluteLayout

可以自己手动去调节位置  但是这种方法太麻烦 (代码繁多 还需要时间去调试)

通过        android:layout_x 和         android:layout_y 去改变位置

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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_x="32dp"
        android:layout_y="53dp"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="146dp"
        android:layout_y="53dp"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="85dp"
        android:layout_y="135dp"
        android:text="Button" />

</AbsoluteLayout>

//.帧布局(FrameLayout)

一帧一帧叠加出来的

布局子视图可以叠加

每个子视图都是一帧

摆放位置只能通过子视图的Android:layout_gravity方法来设置

帧布局是Android系统加载速度最快的布局

就是空间的叠加 

简单列子:

<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"

 xmlns:app="https://schemas.android.com/apk/res-auto"

 xmlns:tools="https://schemas.android.com/tools"

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 tools:context=".zhenbj">

<Button

 android:layout_width="300dp"

 android:layout_height="300dp"

 android:background="@android:color/holo_blue_light"

 android:layout_gravity="center"

 />

 <Button

  android:layout_width="200dp"

  android:layout_height="200dp"

  android:background="@android:color/holo_blue_dark"

  android:layout_gravity="center"

  />

 <Button

  android:layout_width="100dp"

  android:layout_height="100dp"

  android:background="@android:color/holo_green_light"

  android:layout_gravity="center"

  />

</FrameLayout>

//网格布局 一般作用于小格子  不常用 GridLayout

几个重要的属性是:

一开始必须知道有几列 几行 不然默认显示在一行里 

比如:

   android:rowCount="5" //行
    android:columnCount="4"  //列 在开始布局显示的

跨行:

 android:layout_rowSpan="2"  //在控件中用到的

android:layout_gravity="fill_vertical"//需要填充  

填充可能会多出  所以需要

<Space />(用空白代替) 这是个单独的控件

跨列:

android:layout_columnSpan="3"
android:layout_gravity="fill_horizontal"

RelativeLayout相对布局

相对布局是通过相对定位的方式让控件出现在布局任意位置; 

在相对布局中如果不指定控件摆放的位置,那么控件都会被默认放在RelativeLayout的左上角。因此要先指定第一个控件的位置,其他控件为该位置的相对位置;

RelativeLayout属性:

(使用相对布局属性需要先指定控件的id,其他控件根据该控件的id,来确定相对于该控件的相对位置)

1、基本属性
    gravity :设置容器内组件的对齐方式
    ignoreGravity : 设置该属性为true的组件,将不受gravity属性的影响

2、根据父容器定位
    layout_alignParentLeft : 左对齐
    layout_alignParenRight : 右对齐
    layout_alignParentTop : 顶部对齐
    layout_alignParentButtom : 底部对齐
    android:layout_centerHorizontal :水平居中
    android:layout_centerVertical : 垂直居中
    android: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_marginButtom 设置组件离下面的偏移量

5、padding(填充)
    设置组件内部元素间的边距(比如TextView里的字体位置)
    android:padding 往内部元素的上下左右填充一定边距
    paddingLeft 往内部元素的左边填充一定边距
    paddingRight 往内部元素的右方填充一定边距
    paddingTop 往内部元素的上方填充一定边距
    paddingBottom 往内部元素的下发填充一定边距

 

(好懒更新的博客.......加油 明天写权限 先建立表关系)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值