Android基础——布局

这篇博客详细介绍了Android的五种基础布局:LinearLayout的垂直和水平排列,RelativeLayout的相对定位,TableLayout的表格结构,GridLayout的网格分布,以及FrameLayout的层次堆叠。每种布局都讲解了XML属性、创建布局的方法以及展示的效果。

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

LinearLayout线性布局

所有的子元素都按照垂直或水平的顺序在界面上排列

  • 如果垂直排列,则每行仅包含一个界面元素
  • 如果水平排列,则每列仅包含一个界面元素

LinearLayout元素的XML属性

属性名

属性值

备注

layout_width

layout_height

fill_parent

match_parent

布局元素的宽度/高度占满父元素的宽度/高度空间

wrap_content

布局元素的宽度/高度为其内容宽度/高度

数值

数值表示的距离单位

orientation

关键字

布局方向,线性水平布局或垂直布局

layout_weight

数值

使用在View控件中,表示当前

LinearLayout剩余空间在View控件中的分配情况

xml创建布局

<?xml version="1.0" encoding="utf-8"?>
<!--ViewGroup可嵌套ViewGroup
  android:orientation:设置子元素排列方式
  horizontal:表示水平排列
  vertical:表示垂直排列-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注册1"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="登录2"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值