【安卓学习笔记】Android Studio第1课——布局与Hello World!

本文介绍了Android开发中的五大布局类型,并详细解析了相对布局和线性布局的使用方法及属性设置,帮助开发者更好地掌握布局技巧。

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


一、Android五大布局分类

1、相对布局
2、绝对布局
3、线性布局
4、表格布局
5、帧布局

二、具体布局的使用(这里主要介绍相对布局和线性布局)
1、相对布局(RelativeLayout)
可以这样理解:在安卓屏幕中的父元素就是整个屏幕,而子元素就是那些按钮、文本框之类的东西。相对布局内视图可用的常用属性如下:
与位置相关的属性
android:layout_above 表示在目标组件之上
android:layout_below 表示在目标组件之下
android:layout_toLeftOf 表示在目标组件的左边
android:layout_toRightOf 表示在目标组件的右边与对齐相关的属性
android:alignBaseLine 表示与目标组件的基线对齐
android:alignBottom 表示与目标组件的底边对齐
android:alignTop 表示与目标组件的顶边对齐
android:alignLeft 表示与目标组件的左边对齐
android:alignRight 表示与目标组件的右边对齐
android:layout_centerHorizontal 表示在相对布局容器内水平居中
android:layout_centerVertical 表示在相对布局容器内垂直居中


3、线性布局(LinearLayout)
线性布局是一种让视图水平或垂直线性排列的布局,又分为横向和纵向。线性布局按照垂直或者水平的顺序依次排列子元素,每一个子元素都位于前一个元素之后。
1)垂直排列,那么将是一个N行单列的结构,每一行只会有一个元素,而不论这个元素的宽度为多少;
2)水平排列,那么将是一个单行N列的结构。
3)搭建两行两列的结构,通常的方式是先垂直排列两个元素,每一个元素里再包含一个LinearLayout进行水平排列


线性布局的常用属性如下:
android:orientation 表示布局方向
取值vertical表示垂直布局
取值horizontal表示水平布局
android:gravity 表示视图的对齐方式
内容包括:top、bottom、left、right、center_vertical、center_horizontal、center
可以使用|分隔填写多个值
布局中的视图可以使用如下属性
android:layout_gravity 表示单个视图的对齐方式
android:layout_weight 表示单个视图所在大小的比重
当layout_weight为0时,视图大小由自身确定
当layout_weight大于0时,视图在线性布局方向上根据比重拉伸


布局中常用到的关键字:
match_content 包含内容
match_parent 匹配父控件
padding 内边距
margin 外边距

weight 权重 用来分父控件的空间

下面是XML文件Hello World代码,就是一个简单的TextView

<?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">

    <TextView
        android:id="@+id/L1_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hellow World!"
        android:textSize="30sp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textColor="#ff0000"/>

</RelativeLayout>

相关资料来源于网络

By Urien 2018年5月7日 17:49:11


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值