11、Android --LinearLayout布局 基础学习

由于之前已经使用简单介绍下,LinearLayout 就是js中div

属性/设置

  • orientation – 布局中组件的排列方式(vertical–纵向,horizontal-横向)
  • gravity – 控制组件所包含的子元素的对齐方式,可多个组合
  • layout_gravity – 控制该粗件在父容器里的对其方式
  • background – 为该组件设置-一个背景图片,或者是直接用颜色覆盖
  • divider – 分割线,配合showDividers使用(可以是颜色,可以图片,场景类似wx朋友圈中每个信息后面的横线)
  • showDividers – 设置分割线所在的位置;one(无),beginning(开始),end(结束),middle(每两个组件间)
  • dividerPadding – 设置分割线的padding
  • layout_weight – (权重)该属性是用来等比例的划分区域

示例

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:divider="@color/red"
    android:dividerPadding="20px"
    android:showDividers="middle"
    tools:context=".LayoutActivity">
    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="@color/yellow"
        />
    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/green"
        />
    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@color/black"
        />
</LinearLayout>

效果图

在这里插入图片描述

注意事项(weight比例分配)

1、当写有android:layout_weight时,根据布局情况分为两种情况

  • 纵向(android:orientation=“vertical”)建议android:layout_height=‘0dp’
  • 横向(android:orientation=“horizontal”)建议android:layout_width=‘0dp’
    2、如果没有采取1的建议,以纵向为例。

屏幕高度:H (match_parent)
第一、二个LinearLayout默认高度:H (match_parent)
第三个高度:200
则界面剩余高度:H-2H-200 = - H - 200
第一、二个LinearLayout 按照比例分剩余高度:

  • 第一LinearLayout 的高度 = H + (- H - 200) * 2 / 3 = (1/3) H - 400/3
  • 第二LinearLayout 的高度 = H + (- H - 200) * 1 / 3 = (2/3) H - 200/3
  • 第三个LinearLayout 的高度 = 200
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值