LinearLayout控件是Android中重要的布局控件,是一个线性控件,所谓线性控件的意思是指该控件里面的内容仅仅能水平或垂直排列。也就是在一条直线上。

通过控件的属性能够控制该控件内的控件的位置以及大小。以下是放置了3个treeview控件的效果图。此LinearLayout控件的属性为垂直排列.

        

   以下我们通过该控件的几个属性来了解下控件的使用方法

        1.Orientation属性

             该属性设置LinearLayout内的控件显示方向  vertical:垂直显示    horizontal水平显示

                android:orientation="horizontal"

        2. gravity属性

           该属性用来设置LinearLayout内的控件显示位置。 android:gravity="center"表示垂直居中显示.

           该属性有以下枚举值:

           android:gravity="top"
           android:gravity="bottom"
            android:gravity="left"
             android:gravity="right"
            android:gravity="center_vertical"
            android:gravity="fill_vertical"
           android:gravity="fill_horizontal"
           android:gravity="center_horizontal"
           android:gravity="center_vertical"
         android:gravity="fill"
           android:gravity="clip_vertical"
            android:gravity="clip_horizontal"

        

           3.layout_weight属性

                   layout_weight主要是LinearLayout内控件的属性。用来设置LinearLayout内控件的所占比例。

                   3.1  当LinearLayout内的控件垂直排列,而且textview的高度android:layout_height="wrap_content" 时候。我们设置当中一个textview的 android:layout_weight="1",

                     效果例如以下:

                      
                         代码
                        
<?

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"
android:orientation="vertical"
> <TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="标签1"
android:background="#ff0000" /> <TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="标签2"
android:background="#FFB5C5"
/> <TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="标签3"
android:background="#EE82EE" /> </LinearLayout>
                       
                      这里我们来分析下为什么会产生这种效果,首先我们这是在垂直控件上按比例分配,三个标签的layout_height都为wrap_content,,当三个标签按所占的高度分配空间后。剩余空间会按layout_weight比例分配,由于标签1和标签3没有设置layoutweight属性。默觉得0,所以把剩余空间所有分配给了标签2,
                    要谨记一点,此时的textview的  android:layout_height="wrap_content"
            
                   3.2  当我们把三个textview的  android:layout_height="fill_parent", 同一时候把标签1的layout_weight设为1, 标签2和标签3的layout_weight设为2,
           这时我们又会看到不同的效果
                  
                 代码例如以下
                 
<?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"
android:orientation="vertical"
> <TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="标签1"
android:background="#ff0000"
android:layout_weight="1"
/> <TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:text="标签2"
android:background="#FFB5C5"
/> <TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="标签3"
android:background="#EE82EE"
android:layout_weight="2"
/> </LinearLayout>
                  看到这个效果后,大家可能会感觉到疑惑,标签2和标签3的weight值为2。 标签1的weigiht值为1,为什么标签1占的空间要大?
                  这时由于我们把空间的属性设置为fill_parent后layout_height="fill_parent",这时控件所占比例就会按反比例分配控件,
 比例越小,占的空间越大

                 4.商品列表演示样例
                 接下来我们来展示使用LinearLayout做的一个商品列表演示样例,首先在项目中res目录下创建一个raw目录,然后在raw目录放置产品图片,然后我们開始布局,
效果图例如以下
                
               
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
> <LinearLayout android:layout_width="fill_parent" android:layout_height="10dp">
   </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"
android:gravity="center"
>    <LinearLayout
android:layout_width="fill_parent"
android:layout_weight="2"
android:orientation="vertical"
android:layout_height="fill_parent"> <ImageView
android:id="@+id/imageView1"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@raw/pad" />
  </LinearLayout>   <LinearLayout
android:layout_width="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_height="fill_parent"> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商品名称:IPAD Air"
android:layout_weight="1"
/> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="商品价格:$99" /> <TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="商品颜色:白色" />   </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="2dp" android:background="#F0F0F0">    </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"
android:gravity="center"
>    <LinearLayout
android:layout_width="fill_parent"
android:layout_weight="2"
android:orientation="vertical"
android:layout_height="fill_parent"> <ImageView
android:id="@+id/imageView1"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@raw/pad" />
  </LinearLayout>   <LinearLayout
android:layout_width="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_height="fill_parent"> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商品名称:IPAD Air"
android:layout_weight="1"
/> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="商品价格:$99" /> <TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="商品颜色:白色" />   </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="2dp" android:background="#F0F0F0">    </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"
android:gravity="center"
>    <LinearLayout
android:layout_width="fill_parent"
android:layout_weight="2"
android:orientation="vertical"
android:layout_height="fill_parent"> <ImageView
android:id="@+id/imageView1"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@raw/pad" />
  </LinearLayout>   <LinearLayout
android:layout_width="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_height="fill_parent"> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商品名称:IPAD Air"
android:layout_weight="1"
/> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="商品价格:$99" /> <TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="商品颜色:白色" />   </LinearLayout> </LinearLayout> </LinearLayout>





.Net程序猿玩转Android开发---(6)线性布局LinearLayout的更多相关文章

  1. .Net程序猿玩转Android开发---(8)表格布局TableLayout

    表格布局TableLayout是Android中比較经常使用的一个布局控件,既然是表格,肯定有行和列,TableLayout中的行有TableRow组成.列依据每行控件的数量来确定 假如第一行有3个控 ...

  2. .Net程序猿玩转Android开发---(7)相对布局RelativeLayout

                 相对布局RelativeLayout是Android布局中一个比較经常使用的控件,使用该控件能够布局出适合各种屏幕分辨率的布局,RelativeLayout採用相对位置进行 ...

  3. .Net程序猿玩转Android开发---(11)页面跳转

    在不论什么程序开发中,都会遇到页面之间跳转的情况,Android开发也不例外.这一节,我们来认识下Android项目中如何进行页面跳转.页面跳转分为有參数和无參数页面跳转,已经接受还有一个页面的返回值 ...

  4. .Net程序猿玩转Android开发---(3)登陆页面布局

    这一节我们来看看登陆页面如何布局.对于刚接触到Android开发的童鞋来说.Android的布局感觉比較棘手.须要结合各种属性进行设置,接下来我们由点入面来 了解安卓中页面如何布局,登陆页面非常eas ...

  5. Android 自学之线性布局 LinearLayout

    线性布局(LinearLayout),线性布局有点想AWT编程里面的FolwLayout,他们都会将容器里面的组件挨个的排列起来. 他们最大的区别在于:Android的线性布局不会换行:AWT里面的F ...

  6. Android开发之线性布局详解(布局权重)

    布局权重 线性布局支持给个别的子视图设定权重,通过android:layout_weight属性.就一个视图在屏幕上占多大的空间而言,这个属性给其设 定了一个重要的值.一个大的权重值,允许它扩大到填充 ...

  7. .Net程序员玩转Android开发--ListView单击事件

    public class ListViewClickActivity extends Activity {         private ListView lv;        SimpleAdap ...

  8. Android开发-之五大布局

    在html中大家都知道布局是什么意思了,简单来说就是将页面划分模块,比如html中的div.table等.那么Android中也是这样的.Android五大布局让界面更加美化,开发起来也更加方便.当然 ...

  9. 安卓开发06:布局-线性布局 LinearLayout

    LinearLayout把视图组织成一行或一列.子视图能被安排成垂直的或水平的.线性布局是非常常用的一种布局方式. 请看一个布局例子: <LinearLayout xmlns:android=& ...

随机推荐

  1. Elasticsearch之Hadoop插件的安装(图文详解)

    这个Hadoop插件的安装是非常重要. Hadoop插件安装 在es的安装目录下 bin/plugin install elasticsearch/elasticsearch-repository-h ...

  2. sql 循环分割字符

    DECLARE @Items VARCHAR(1000)='148' --待处理拼接字符串 --开始处理SET @Items=@Items+',' --必须追加“,”否则最后一个无法输出DECLARE ...

  3. Jquery EasyUI环境下设置下拉框选中指定选项

    前提: 要求点击某个按钮就将所有的下拉框都设置为选中第一个选项,因此,指定索引是最好的做法 尝试过的做法: html代码如下(easyui 的写法) <select class="ea ...

  4. [ SCOI 2008 ] 着色方案

    \(\\\) \(Description\) 给出\(K\)种颜料各自的个数\(C_i\),每一个颜料只够涂一个格子,求将颜料用完,涂一排格子,每个格子只能涂一次的条件下,相邻两个格子的颜色互不相同的 ...

  5. Raspberry Pi开发之旅-光照强度检测(BH1750)

    一.前期准备 1.环境要求 GY30模块(BH1750FVI传感器),树莓派系统,python-smbus,iic开启 2.取消对IIC驱动的黑名单 nano /etc/modprobe.d/rasp ...

  6. VMWare 安装Ubuntu 16.04

    1.新建虚拟机 (1)点击文件-->新建虚拟机 (2)选择 自定义(高级)--> 下一步 (3)选择Workstation 12.0 --> 下一步 (4)选择 稍后安装操作系统 - ...

  7. 认识 java JVM虚拟机选项 Xms Xmx PermSize MaxPermSize 区别

    点击window---->preferences---->配置的tomcat---->JDK,在Optional Java VM arguments:中输入 -Xmx512M -Xm ...

  8. 实验2 C++数组与指针

    一.实验目的: 掌握一维数组和二维数组的定义.赋值和输入输出的方法. 掌握字符数组和字符串函数的使用. 通过实验进一步掌握指针的概念,会定义和使用指针变量. 能正确使用数组的指针和指向数组的指针变量. ...

  9. R包

    查看默认安装包的位置 .libPaths() 移除包 remove.packages("package_name") 查看所有安装的包 library() 按 q 退出包列表   ...

  10. react 子组件调用父组件方法

    import React from 'react'import '../page1/header.css'import { Table } from 'antd'import Child from ' ...