详解android四种布局之LinearLayout

本文深入探讨了Android开发中常用的LinearLayout布局特性。包括垂直与水平排列方式、控件对齐方法及如何利用权重属性实现灵活的空间分配。

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

第一种布局: LinearLayout

    LinearLayout又称作线性布局,是一种非常常用的布局。正如它名字所描述的一样,这 个布局会将它所包含的控件在线性方向上依次排列。
    既然是线性排列,肯定就不仅只有一个方向。我们通过 android:orientation属性指定了排列方向。

1. Linearlayout的android:orientation属性

1.1 “vertical” 垂直分布

    当设置成vertical时,不能设置android:layoy_height属性设置为match_parent,否则其他控件都将被覆盖

1.2 “horizontal” 水平分布 ——默认值

    当设置成horizontal时,不能设置android:layoy_width属性设置为match_parent,否则其他控件都将被覆盖

2. 在LinearLayout的布局中,控件与布局相关的属性

2.1 android:gravity 指文字在控件中的对齐方式

2.2 android:layout_gravity 指控件在布局中的对其方式

    layout_gravity的值有三种:bottom、top、center_vertical
    需要注意,当 LinearLayout的排列方向是 horizontal时,只有垂直方向上的对齐方式才会生效,因为此时水 平方向上的长度是不固定的,每添加一个控件,水平方向上的长度都会改变,因而无法指定该方向上的对齐方式。
    同样的道理,当 LinearLayout的排列方向是 vertical时,只有水平方 向上的对齐方式才会生效。

xml文件中的代码

<?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/button1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="top" 
        android:text="Button 1" /> 
    <Button 
        android:id="@+id/button2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_vertical" 
        android:text="Button 2" /> 
    <Button 
        android:id="@+id/button3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom" 
        android:text="Button 3" />  
</LinearLayout>

当布局为水平布局时,空间选择在布局文件中的对齐方式分别为上中下对齐的效果效果图:
这里写图片描述

2.3 android:layout_weight 这个属性允许我们使用比例的方式来指定控件的大小

    当设置了layout_weight属性后,layout_width/layout_height属性的值若为数字将失效(其余都覆盖layout_weight属性),一般设置了layout_weight的属性后,若是水平布局,控件的高度不可设置为0dp,若是垂直布局,控件的宽度不可以设置为0dp。
    空间分配:系统会先把 LinearLayout下所有控件指定的 layout_weight值相加,得到一个总值, 然后每个控件所占大小的比例就是用该控件的 layout_weight值除以刚才算出的总值。

Width为数值时,width效果被weight覆盖
这里写图片描述
Width为数值时,width效果被weight覆盖,xml文件中的代码

<?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" >   
    <EditText 
        android:id="@+id/input_message" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:hint="Type something" />

    <Button 
        android:id="@+id/send" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:text="Send" />  
</LinearLayout>

设置为wrap_content时……weight被覆盖
这里写图片描述

<?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" >
    <EditText 
        android:id="@+id/input_message" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:hint="Type something" />   
    <Button 
        android:id="@+id/send" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:text="Send" />  
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值