Android5种布局类型

本文详细介绍了Android的五种布局类型:LinearLayout, RelativeLayout, FrameLayout, TableLayout和AbsoluteLayout。LinearLayout支持水平和垂直方向的控件排列,RelativeLaout依赖于相对定位,FrameLayout是最简单的布局,TableLayout以表格形式展示控件,而AbsoluteLayout则允许精确的X,Y坐标定位。了解每种布局的特点和属性,能帮助开发者创建满足需求的界面设计。" 103781838,6676232,使用Go进行RSA数字签名实战,"['Go编程', '加密算法', '网络安全', 'RSA算法']
摘要由CSDN通过智能技术生成

为了适应不同界面的风格,android为开发人员提供了5种常用布局,分别是LinearLayout(线性布局)、RelativeLayout(相对布局)、FrameLayout(帧布局)、TableLayout(表格布局)/AbsoluteLayout(绝对布局)。

1.LinearLayout(线性布局)

首先我们来介绍线性布局,线性布局是在实际开发中比较常用的,他主要以水平和垂直的方式来显示界面中的控件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A"
       />
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="B"
        />
    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="C"
        />

</LinearLayout>

首先是layout_width和layout_height这两个控件属性,这两个值可以设置为wrap_content(控件大小由控件内容大小变化而变化)和match_parent(填满父窗体由父容器决定控件大小),也可以自定义大小。

改变btn3的layout_width的值使他变成match_parent:
在这里插入图片描述
改变三个按钮的layout_width的值,自定义为100dp、200dp、300dp:
在这里插入图片描述
同理layout_height也是可以一样根据自己的需求自己定义自己需要的高度大小。
在这里插入图片描述
之后是orientation一个关键的属性,用于控制控件的排列方向,它有两个值 vertical表示线性布局垂直显示,horizontal表示线性布局水平显示。

将orientation的值变为vertical就会变成水平显示:
在这里插入图片描述
你也可以根据需要使用嵌套线性布局即在线性布局中再使用线性布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A"
       />
    <LinearLayout
        android:layout_width=
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值