笔记三:常用布局:线性布局——LinearLayout

前言

程序的界面可以简单也可以错综复杂,可以朴实无华也可以花里胡哨,程序界面的的美观完全取决于各个控件的摆放位置,而存放这些控件的容器 被称为布局(Layout),每个活动都必须有自己的布局文件,开发人员可以在布局内按照一定的要求或者规律来摆放控件,从而实现精美界面的目的。一个布局是可以嵌套另一个布局的,这样就是我们能够完成一些更加复杂的精美的界面,嵌套原则:①根布局管理器必须要有一个xmlns文件;②在一个布局文件中,最多只能有一个根布局管理器,如果需要有多个还需要用一个根布局管理器,将他们括起来即可;③不能嵌套太深,会影响程序性能。

线性布局(LinearLayout)

LinearLayout 图片资源(图片来自菜鸟教程)
顾名思义,线性布局强调布局将所有控件在线性方向上排列,一般分为竖直方向和水平方向,在编码时android也提供了转向的语句。当想改变线性布局控件排列方式时,可利用android:orientation属性更改,竖直方向用android:orientation=“vertical”;水平方向则用“horizontal”。具体看代码:

<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"
    tools:context=".Linearlayout"
    android:orientation="vertical">//竖直方向
    <Button
        android:id="@+id/bt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮一"
        android:textSize="20sp"/>//text:决定按钮上的文字;textSize:决定字体大小(sp)
    <Button
        android:id="@+id/bt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮二"
        android:textColor="@color/bg_blue"/>//textColor:决定字体颜色
</LinearLayout>

android:layout_width:用来设定控件的宽度,一般分为match_parent(适应屏幕)和wrap_content(根据内容决定),也可以自己设定宽度如100dp、200dp等,空间大小只能用dp表示。android:layout_height表示方法与android:layout_width一致。
通常在控件署名时,常用android:text=“控件名”表示,字体的大小用android:textSize=“**sp”表示,字体大小只能用sp表示,字体颜色用android:textColor=“color”表示,可以直接输入色号,也可以提前在res->values中定义颜色便可直接调用,先右击values建立一个color.xml文件输入一下代码则可实现颜色绑定:

<resources>
	<color name="bg_blue">#58ACED</color>
</resources>

实现结果如下:
实现结果
在之前的基础上再嵌套一个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"
    tools:context=".Linearlayout"
    android:orientation="vertical">//竖直方向
    <Button
        android:id="@+id/bt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮一"
        android:textSize="20sp"/>//text:决定按钮上的文字;textSize:决定字体大小(sp)
    <Button
        android:id="@+id/bt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮二"
        android:textColor="@color/bg_blue"/>//textColor:决定字体颜色
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <Button
            android:id="@+id/bt3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮三"/>
        <Button
            android:id="@+id/bt4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮四"/>
    </LinearLayout>
</LinearLayout>

运行结果如下:
嵌套

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值