LinearLayout

概念:

LinearLayout是一种线性布局,他会将控件在水平和垂直方向做线性排列

官方文档:

http://developer.android.com/guide/topics/ui/layout/linear.html

http://developer.android.com/reference/android/widget/LinearLayout.html

继承关系:

常用属性:

android:orientation:可以设置布局的方向

android:gravity:用来控制组件的对齐方式

layout_weight:控制各个组件在布局中的相对大小  同一线性上当前控件所占比例长度

样例一:

代码

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_alignParentTop="true"    android:layout_alignParentLeft="true"    android:layout_alignParentStart="true">

<EditText        android:layout_width="0dp"        android:layout_height="wrap_content"        android:id="@+id/editText2"        android:layout_weight="4" />

<Button        android:layout_width="0dp"        android:layout_height="wrap_content"        android:text="send"        android:id="@+id/btn_send"        android:layout_weight="1" /></LinearLayout>

android:layout_width="0dp"  使得宽度不依赖于layout_width

当前同一水平线上有2个控件  所以layout_weight = 4 表示其宽度占屏幕宽度的4/5

案例二:

LinearLayout嵌套

Code:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <LinearLayout        android:orientation="horizontal"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:baselineAligned="false"        android:layout_weight="1"  >

<LinearLayout            android:orientation="horizontal"            android:layout_width="wrap_content"            android:layout_height="fill_parent"            android:layout_weight="1">            <TextView                android:text="green"                android:textColor="#ff0000"                android:background="#00aa00"                android:layout_width="wrap_content"                android:layout_height="fill_parent"                android:layout_weight="1"/>            <TextView                android:text="blue"                android:background="#0000aa"                android:layout_width="wrap_content"                android:layout_height="fill_parent"                android:layout_weight="1"/>        </LinearLayout>        <LinearLayout            android:orientation="vertical"            android:layout_width="wrap_content"            android:layout_height="fill_parent"            android:layout_weight="1">            <TextView                android:text="black"                android:background="#000000"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:layout_weight="1"/>            <TextView                android:text="yellow"                android:background="#aaaa00"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:layout_weight="1"/>            <TextView                android:text="unknown"                android:background="#00aaaa"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:layout_weight="1"/>        </LinearLayout>    </LinearLayout>    <LinearLayout        android:orientation="vertical"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="2">        <TextView            android:text="color_red"            android:gravity="fill_vertical"            android:background="#aa0000"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="2"/>        <TextView            android:text="white"            android:textColor="#ff0000"            android:background="#ffffff"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="2"/>    </LinearLayout>

</LinearLayout>

案例三:

android:gravity:用来控制组件的对齐方式

每一个按钮独自占一列

Code:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">

<Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="New Button"        android:id="@+id/button3"        android:layout_gravity="bottom|right|top"        android:layout_weight="1"/>

<Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="New Button"        android:id="@+id/button2"        android:layout_gravity="center_vertical"        android:layout_weight="1"/>

<Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="New Button"        android:id="@+id/button"        android:layout_gravity="bottom"        android:layout_weight="1" /></LinearLayout>

来自为知笔记(Wiz)

时间: 2024-08-24 15:56:26

LinearLayout的相关文章

【黑马Android】(04)数据库的创建和sql语句增删改查/LinearLayout展示列表数据/ListView的使用和BaseAdater/内容提供者创建

数据库的创建和sql语句增删改查 1. 加载驱动. 2. 连接数据库. 3. 操作数据库. 创建表: create table person( _id integer primary key, name varchar(20), age integer ); 添加: insert into person(name, age) values('lisi', 19); 删除: delete from person where _id = 1; 修改: update person set name =

LinearLayout设置 weight 无法绘制的问题

项目地址:ChildLayout 一. 问题起因: 新项目中首页有这么一个需求:看布局: 主要就是"大牌专场"这个栏目的布局问题,呈现一个,左--右上--右下 的形式,因为不能像 iOS 那样直接根据 UED 给的标注来写死布局尺寸,Android 混乱的分辨率决定了这个布局需要采用 android:layout_height="wrap_content" 的形式来决定它的高. 二. 解决方法1:使用 LinearLayout--失败 要画这个布局很简单的,比例都是

android布局----LinearLayout布局方式

线性布局,控件成直线方式排列,要么水平排列,要么垂直排列. 对着layout文件夹右键,然后选择新建android xml file,选择资源类型选择 layout --> 根节点选择 LinearLyout <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu

Android学习——LinearLayout布局实现居中、左对齐、右对齐

android:orientation="vertical"表示该布局下的元素垂直排列: 在整体垂直排列的基础上想要实现内部水平排列,则在整体LinearLayout布局下再创建一个LinearLayout布局. 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/r

Android的学习第六章(布局一LinearLayout)

今天我们来说一下Android五大布局-LinearLayout布局(线性布局) 含义:线性布局,顾名思义,指的是整个Android布局中的控件摆放方式是以线性的方式摆放的, 主要作用:主要对整个界面进行基本设置使用 重要属性:android:orientation 值:horizontal 元素水平摆放  |  vertical 元素垂直摆放 看代码: <!-- 第一个线性布局, 我们可以视为html中的div,用于对于整个界面进行布局 这里面 xmlns:android和xmlns:tool

Fragemnt和TextView的交互(TextView在LinearLayout中)

import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.os.Bundle;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.View;import android.wid

和我一起看API(一)你所不知道的LinearLayout

楼主英语水平差,翻译的不好的话请多多指正,嘿嘿... A Layout that arranges its children in a single column or a single row. The direction of* the row canbe set by calling {@link #setOrientation(int) setOrientation()}.* You can also specify gravity, which specifies thealignme

寒假学干货之------LinearLayout.layout.weight

所有原始代码由这个大神写的--http://www.cnblogs.com/zhangs1986/archive/2013/01/17/2864237.html layout/activity_main下 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:i

第7章(3) LinearLayout(线性布局)

分类:C#.Android.VS2015: 创建日期:2016-02-10 一.简介 LinearLayout将容器内的组件一个挨着一个地横向或纵向依次堆叠起来(不重叠).该布局和WPF的StackPanel控件的功能非常相似,也是通过orientation属性设置排列的方向是纵向(vertical)还是纵向(horizontal). 常用属性有: android:layout_gravity:子元素在容器中的对齐方式.即:往哪一端偏沉(gravity:重力). android:orientat

Android开发--LinearLayout的应用

1.简介 LinearLayout为安卓三大常用布局中的线性布局.其中,线性布局又分为水平线性布局和垂直线性布局.视图如下所示:                 水平布局          垂直布局 2.构建 在安卓布局中,往往需要我们进行嵌套布局,以下图为例                             重点如下所示: 2.1 android:orientation="horizontal/ertical":该属性表示布局为水平方式或垂直方式. 2.2 android:la