Android布局(一)——线性布局LinearLayout

今天来讲一下布局的知识,首先常用的就是线性布局,因为拿到规划图第一个要写的就是布局,能不能设计出心里想要的结果,全看自己对于布局的掌握如何,所以说布局还是有一定的重要性。

 

注意的是,这个文章只讲了大概的使用办法,具体还需多加练习,有问题可以留言或者私信。

 

布局管理器LinearLayout——线性布局

1.最常用的属性

android:id  布局的标识

android:layout_width:布局的宽度

android:layout_height:布局的高度

android:background:布局的背景,可选颜色,图片等

android:layout_margin:布局的外边距

android:layout_padding:布局的内边距

android:orientation:布局内的排列方式

2.一般的线性布局的常见的定义

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CustomDialogActivity"
    android:padding="15dp"
    android:background="@color/colorDark">

    <Button
        android:id="@+id/bt_custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自定义dialog"
        />

</LinearLayout>

效果如图所示:

3.布局嵌套布局

也可以在布局中套布局如下代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CustomDialogActivity"
    android:padding="15dp"
    android:background="@color/colorDark">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="@color/colorAccent">
        <Button
            android:id="@+id/bt_custom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="自定义dialog"
            />
    </LinearLayout>
</LinearLayout>

在第一个线性布局中又写了一个线性布局,效果如下图所示

4.一些属性介绍

(1)关于android:orientation属性

vertical 垂直排列

horizontal水平排列

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CustomDialogActivity"
    android:padding="15dp"
    android:background="@color/colorDark"
    android:orientation="vertical">  //两个线性布局是垂直排列

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="@color/colorAccent"
        android:orientation="horizontal" //里面的组件是水平排列
        android:layout_marginBottom="30dp">
        <Button
            android:id="@+id/bt_custom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自定义dialog"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自定义dialog"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="@color/colorAccent"
        android:orientation="vertical">//里面的组件是水平排列
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自定义dialog"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自定义dialog"
            />
    </LinearLayout>

</LinearLayout>

效果如图所示,具体可以自己看代码理解,学习效果最明显

(2)android:gravity的属性设置,是标志布局内的组件放在什么位置,如下代码

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="@color/colorAccent"
        android:orientation="vertical"
        android:gravity="center">//放在中间位置进行排列
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自定义dialog"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自定义dialog"
            />
    </LinearLayout>

效果如图,其他放置位置可以自己选择

(3)android:weight权重的意思,可以去根据权重来分割整个布局的长度或者宽度

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="@color/colorAccent"
        android:orientation="horizontal"
        android:layout_marginBottom="30dp">

        <view
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#000000"
            android:layout_weight="1">

        </view>
        <view
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@color/colorAccent"
            android:layout_weight="1">

        </view>
    </LinearLayout>

效果如下图所示,两个view的weight都是1,则两个各占布局的1/2,等用的时候可以实践一下

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Demo.demo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值