Android Studio 学习记录-布局管理器-线性布局(LinearLayout)

       

目录

最常用属性:

边练边学:

实例练习1(LinearLayout):

实例练习2(android:padding):

实例练习3(match_paren):

实例练习4(vertical/layout_margin):

实例练习5(padding2):

实例练习6(gravity):

实例练习7(layout_weight):

实例练习8(layout_weight2):


         线性布局(Linear Layout)是使用最为频繁的布局方法之一,之后将边练习边学习相关知识。

最常用属性:

android:id                                        相当于控件的标识,可以通过id来找到控件

android:layout_width                       宽度

android:layout_height                      高度

android:layout_margin                     外边距,控件距离外部元素的编剧

android:layout_padding                   内边距,控件距离外部元素的编剧

android:background                         背景,可以为颜色、图片、自定义xml文件

android:orientation                           方向,设定线性布局方向

边练边学:

实例练习1(LinearLayout):

        将根布局设为线性布局(LinearLayout):

        高度与宽度“match_parent”为匹配父控件,上一级控件宽、高有多少,宽、高就有多少【“wrap_content” 为包含内容,内容有多少,宽度、高度就有多少】

        方向“vertical”为垂直方向【“horizont”为水平方向】

        添加一个为线性布局(LinearLayout)的子元素:

        赋予id:ll_1(@+id指创建一个id)

        宽度为“match_parent”匹配父控件,高度为“150dp”(安卓屏幕碎片化比较严重,分辨率不同,一般使用dp,根据屏幕分辨率适配大小)

        方向“horizont”为水平方向

        背景设置颜色色号#028702

<?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"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/ll_1"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="horizontal"
        android:background="#028702"
        android:padding="20dp">
    </LinearLayout>
</LinearLayout>

实例练习2(android:padding):

        添加一个副控件<View>

        宽度“100dp” ,高度为“match_parent”匹配父控件

        背景设置颜色色号#ff0033

        父控件中设置“android:padding=“20dp” ”(内边距20dp,里边内容需距离边框有20个单位)

<View
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:background="#ff0033" />
  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值