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. Unity基本操作

    主要内容: C#学习 Unity项目 打砖块:BreakBricks Unity操作 Unity操作: 调试 碰撞体 触发器 视角 键盘视角平移 光照贴图 游戏对象Gameobject 访问对象 实体 ...

  2. git配置用户信息

    git配置用户信息 一.在本地配置用户信息 配置内容:user.name 和 user.email 配置目的:Git用来记录谁做了什么事 配置方法:windows下打开Git Bash进行操作  gi ...

  3. $P2299 Mzc和体委的争夺战$

    \(problem\) #ifdef Dubug #endif #include <bits/stdc++.h> using namespace std; typedef long lon ...

  4. azkaban-executor启动时出现conf/global.properties (No such file or directory)的问题解决(图文详解)

     问题详情 // :: INFO [FlowRunnerManager] [Azkaban] Cleaning recently finished // :: INFO [FlowRunnerMana ...

  5. [ NOIP 2002 ] TG

    \(\\\) \(\#A\) 均分纸牌 有\(N\)堆纸牌,每堆有若干张,但纸牌总数必为\(N\)的倍数.可以在任一堆上取若干张纸牌,然后移动给其左右任意一侧的纸牌堆,求将所有的牌堆牌数都变为平均值最 ...

  6. CentOS6.5下简单的MySQL数据库操作

    1.登录成功之后退出的话,直接输入quit或者exit即可.

  7. ajax不跳转页面的快速删除操作,可添加美观样式

    以前我们讲的删除是利用嵌入php代码,跳转到另一个页面,从而降低了删除速度,但我们今天讲的利用ajax不仅可以达到不跳页面快速删除,并且能添加特效来美化页面. 上代码,我们先来做主页面 <!DO ...

  8. Resources.getResourceAsReader 报空指针

    1.maven项目中 可能未编译 导致找不到, 解决方法:mvn clean install -DskipTests -X   编译一下项目 2.可能在 Resources.getResourceAs ...

  9. C# textbox 获得焦点

    this.ActiveControl = txt_core;

  10. mvc 类中对应数据库属性

    [StringLength()] //可空 对应数据库可空 [DefaultValue("")] [DisplayName("添加人用户名")] public ...