五大布局——线性布局

1.linearLayout的属性

线性布局是程序中最常见的一种布局方式,线性布局可以分为水平线性>布局和垂直线性布局两种,通过android:orientation属性可以设置线性布局的方向。下面是一个简单的线性布局的例子:
1.android:oritation=”vertital” 竖向布局
2.android:oritation=“horizontal”横向布局(默认的)
3.
(1).px——像素
(2).dp(dip(Device-independent pixel,设备独立像素))——可作长 度单位,不同设备有不同的显示效果,这个和设备硬件有关,dp和具体像素值的对应公式是dp值 =设备密度/160* 像素值,可以看出在dpi(像素密度)为160dpi的设备上1px=1dip
(3).sp(ScaledPixels 放大像素)——用于表示文本的大小,跟dp一样; 只能用于表示文本。
4. android:weightSum=”4”> //表示在线性布局中将空白部分分为4份
android:layout_weight=”1” //占了一份
5.eg: android:marginleft=“15dp”//离开左端15dp
android:gravity=”center” //布局在中心位置

2.线性布局的一个例子

布局效果如下图所示:
这里写图片描述
代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="3">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="3">
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/blue"/>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:orientation="vertical"
            android:weightSum="2">
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@color/green"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@color/yellow"/>
        </LinearLayout>

    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/red"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="3"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:weightSum="3"
            >
            <Button
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
                android:background="@color/red"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@color/blue"/>/>
            <Button
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
                android:background="@color/yellow"/>/>

        </LinearLayout>
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="@color/green"/>
    </LinearLayout>

</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值