Android笔记(二)布局二:LinearLayout线性布局

本文深入解析Android开发中线性布局(LinearLayout)的使用,包括其默认的水平排列特性及如何通过android:orientation属性调整为垂直排列。同时,详细阐述了权重属性android:layout_weight的作用,展示了如何通过权重比分配视图的空间,特别是在剩余空间的等比例划分中。

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

LinearLayout线性布局

一、线性布局的介绍

1、LinearLayout属性为“线性布局”:着排列和水平排列都为线性排列。

2、LinearLayout默认为水平排列。

如下代码所示我们观察一下,LinearLayout默认排布的样式:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AAAAAAAA" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BBBBBBBB" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CCCCCCCC" />

</LinearLayout >

可以观察到:AAAAAAAA\BBBBBBBB\CCCCCCCC是以水平方向排布的。 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"  >
     <!-- 在这里增加一个 排布的属性:vertical -->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AAAAAAAA" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BBBBBBBB" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CCCCCCCC" />

</LinearLayout>

通过运行的结果我们发仅仅是android:orientation="vertical"属性的加入就变成了纵向排列。

二、权重属性 

我们再来看一个关键的属性:权重比属性:android:layout_weight=""

android:layout_weight后面的一对冒号 “”,中填写数字,这个数字单独看没有什么意义,但是多个android:layout_weight属性中的值会相加,然后作为单个权重属性的分母,所得的值来去划分本View在排布中的比例。

这么说有点蒙,我们举个例看一看吧。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"  >
     <!-- 在这里增加一个 排布的属性:vertical -->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="AAAAAAAA" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="BBBBBBBB" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="CCCCCCCC" />

</LinearLayout>

上述代码可以看到,灭个TextView我们都添加了android:layout_weight="1",也就是说3个TextView都是android:layout_weight="1",这样1+1+1=3,每个TextView占据的区域为1/3窗体的面积,有因为我们之前有纵向排列的属性:android:orientation="vertical"

因此我们猜测,三个TextView纵向瓜分视图的区域:运行如下

果真如此。

 但是值得注意的是:权重比属性android:layout_weight是受到属性android:layout_height限制的(目前并不受到 android:layout_width的限制,因为目前是纵向排布,纵向排布是跟“高度”有关系的跟“宽度”没有关系)

我们把第一个TextView的android:layout_weight属性改成300dp运行再看看效果:

......
<TextView
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:layout_weight="1"
        android:text="AAAAAAAA" />
......

我们来分析一下:我用的是夜神模拟器(为什么不用Android studio自带的模拟器:电脑配置太低了,我连as都不敢用,作为嵌入式设备上的Android不用跟着最新版本跑,低版本过时的Android其实研习价值也非常高,后续公司换一批高性能电脑我在穿插把最近用的as3.3的讲解给补上),屏幕设置的大小540*960.纵向高度为960dp,第二个和第三个TextView加起来是960-300=660dp。如果权重比还是刚才的“1”的话,最后的660会均分成330dp。

综上所述,android:layout_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="vertical" >

    <LinearLayout
        	android:layout_width="match_parent"
	        android:layout_height="match_parent"
	        android:layout_weight="1"
	        android:orientation="vertical" >
	    <TextView
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"
	        android:layout_weight="1"
	        android:background="#F0F"
	        android:text="TextView" />
	     <TextView
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"
	         android:layout_weight="1"
	        android:background="#0FF"
	        android:text="TextView" />
	</LinearLayout>
	
	<LinearLayout
        	android:layout_width="match_parent"
	        android:layout_height="match_parent"
	        android:layout_weight="1"
	        android:orientation="horizontal" >

		<TextView
		    android:layout_width="match_parent"
		    android:layout_height="match_parent"
		    android:layout_weight="1"
		    android:background="#F00"
		    android:text="TextView" />

	     <TextView
	        android:layout_width="match_parent"
	        android:layout_height="match_parent"
	         android:layout_weight="1"
	        android:background="#0F0"
	        android:text="TextView" />
	     <TextView
	        android:layout_width="match_parent"
	        android:layout_height="match_parent"
	         android:layout_weight="1"
	        android:background="#00F"
	        android:text="TextView" />
	</LinearLayout>
	
	<LinearLayout
        	android:layout_width="match_parent"
	        android:layout_height="match_parent"
	        android:layout_weight="1"
	        android:orientation="vertical" >
	    <TextView
	        android:layout_width="match_parent"
	        android:layout_height="match_parent"
	        android:layout_weight="1"
	        android:background="#0FF"
	        android:text="TextView" />
	     <TextView
	        android:layout_width="match_parent"
	        android:layout_height="match_parent"
	         android:layout_weight="1"
	        android:background="#FF0"
	        android:text="TextView" />
	</LinearLayout>
</LinearLayout>

运行视图:

三、总结

1、本节其实就主要讲解了两个重要的常用的点:线性布局和权重布局。

2、布局的属性标记有很多,需要在应用真实项目中慢慢体会,多测试。没必要死记。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值