Android布局-LinearLayout

来源:互联网 发布:全自动挂机赚钱软件 编辑:程序博客网 时间:2024/05/29 08:32

首先,文章我并不保证是百分百的原创,可能会借鉴部分内容,借鉴的内容我会提供原连接,但是文章的代码,我肯定是亲自调试过的。如果你有什么问题,可以在下面进行评论,我看到后第一时间进行回复。

下面是干货:

LinearLayout布局的特点就是垂直布局和水平布局了,用的非常多

orientation
布局的排列方式
参数:
vertical
垂直排列
horizontal
水平排列

这里写图片描述

代码

<?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=".SimpleLinearLayout">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:layout_margin="20dp"        android:text="水平布局"        android:textSize="20sp"        />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:layout_margin="20dp"        >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="姓名:"            android:textSize="20sp"            />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="桑博"            android:textSize="20sp"            />    </LinearLayout>    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:layout_margin="20dp"        android:text="垂直布局"        android:textSize="20sp"        />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        android:layout_margin="20dp"        >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="地址:"            android:textSize="20sp"            />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="山东省济南市blablablablabla"            android:textSize="20sp"            />    </LinearLayout>    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:layout_margin="20dp"        android:text="水平+垂直布局"        android:textSize="20sp"        />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal"            android:layout_marginLeft="20dp"            >            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="姓名:"                android:textSize="20sp"                />            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="桑博"                android:textSize="20sp"                />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal"            android:layout_marginLeft="20dp"            >            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="性别:"                android:textSize="20sp"                />            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="男"                android:textSize="20sp"                />        </LinearLayout>    </LinearLayout></LinearLayout>

1.在LinearLayout中设置排列方式为水平时只有垂直方向的设置是有效的,水平方向的设置是无效的:即left,right,center_horizontal 是不生效的。

2.在LinearLayout中设置排列方式为垂直时只有水平方向设置是有效的,垂直方向的设置是无效的是无效的:即top,bottom,center_vertical 是无效的。
参考:http://www.cnblogs.com/xieyong_198510/p/3463454.html

简单的介绍这些布局的属性,其他布局将不再介绍。
1、LinearLayout的属性
layout_width 布局的宽度
layout_height 布局的高度
参数:
match_parent
根据父布局的长度有多大,自身就有多大
wrap_content
自适应长度,适应自身及父控件的最大长度
fill_parent
撑满,已经弃用

layout_gravity
布局内控件方向,挑几个简单的说明一下
top,bottom,left,right
让对象在其容器的顶部,底部,左边,右边
center
让对象在其容器的中心位置
center_vertical
垂直居中
center_horizontal
水平居中

参考:
https://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html

layout_margin
布局边框的额外空间,包含上下左右(布局1与布局2相隔的距离)
layout_marginRight,layout_marginLeft,layout_marginTop,layout_marginBottom
单独设置右,左,上,下,布局边框的额外空间

padding
布局与其容器内空间的距离,包含上下左右
也可以单独设置如:paddingRight

background
背景颜色,可以设置图片,颜色为背景。

0 0
原创粉丝点击