Android UI 开发入门—线性布局

这篇博客介绍了Android中的线性布局,包括水平和垂直布局的使用。通过在Android Studio中创建XML文件并利用android:layout_weight属性来实现按比例分配空间,以提升界面在不同屏幕尺寸上的适配性。

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

线性布局(LinearLayout)

  线性布局是Android中较为常用的布局方式,它使用<LinearLayout>标签。线性布局主要有两种形式,一种是水平线性布局,一种是垂直线性布局,如图所示:



1.在Androidstudio开发环境中建立UI界面切换至Android视图依次打开app-res-layout,在layout中选择新建XML文件(linearlayout_demo1.xml)编写代码,通过修改代码进行线性布局的水平或者垂直两种形式的转换,其代码如下:

  <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"
        />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button3"
    />
2.然后建立名为linearlayout_demo2.xml的XML文件进行UI界面中控件的位置变换,代码如下:
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:text="Button1"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Button2"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:text="Button3"
    />



3.重要属性—android:layout_weight

 android:layout_weight属性允许我们使用比例的方式来指定空间的大小,它在手机屏幕的适配性方面可以起到非常重要的作用。

 

建立名为linearlayout_demo3.xml的XML文件运用android:layout_weight属性编写如下代码:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:hint="Type someting" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Send" />


 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值