LinearLayout(线性布局)

本文详细介绍了在Android开发中如何使用LinearLayout的weight属性来实现组件按比例分配空间,以及如何为LinearLayout设置分割线,提升界面美观度。

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

1.weight(权重)属性详解

要实现第一个的1:2:3的效果,只需要分别把三个LinearLayout的weight改成1,2和3就可以了 用法归纳: 按比例划分水平方向:

android:orientation="horizontal"

宽高设计如下:

android:layout_width="wrap_content"
android:layout_height="fill_parent"

然后设置为android weight属性设置比例即可
代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

      <TextView
          android:layout_weight="1"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:text="one"
          android:background="#98FB98"
          />
      <TextView
          android:layout_weight="2"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:text="two"
          android:background="#FFFF00"
          />
      <TextView
          android:layout_weight="3"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:text="three"
          android:background="#FF00FF"
          />
</LinearLayout>

效果图如下:

2.为LinearLayout设置分割线

很多界面开发中都会设置一些下划线,或者分割线,从而使得界面更加整洁美观,比如下面的酷狗 音乐的注册页面:
在这里插入图片描述
对于这种线,我们通常的做法有两种 ①直接在布局中添加一个view,这个view的作用仅仅是显示出一条线,代码也很简单:

<View  
    android:layout_width="match_parent"  
    android:layout_height="1px"  
    android:background="#000000" />  

②第二种则是使用LinearLayout的一个divider属性,直接为LinearLayout设置分割线 这里就需要你自己准备一张线的图片了 1)android:divider设置作为分割线的图片 2)android:showDividers设置分割线的位置,none(无),beginning(开始),end(结束),middle(每两个组件间) 3)dividerPadding设置分割线的Padding

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值