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. Java线程之Synchronized用法

    synchronized是Java中的关键字,是一种同步锁.它修饰的对象有以下几种: 修饰一个代码块,被修饰的代码块称为同步语句块,其作用的范围是大括号{}括起来的代码,作用的对象是调用这个代码块的对 ...

  2. phpStorm更新后配置svn无法使用

    phpStorm迎来新的更新,结果之前配置的svn竟然无法使用啦,快捷键一类也没有作用.各种查找解决方案,最后找到解决方案.点击File找到Settings,找到Plugins这个部分,这个部分是管理 ...

  3. --NSArray与NSMutableArray用copy修饰还是strong(转)

    一.NSMutableArray 被copy.strong修饰后的变化: 把NSMutableArray用copy修饰有时就会crash,因为对这个数组进行了增删改操作,而copy后的数组变成了不可变 ...

  4. BZOJ 4173 数论

    思路: $(m%k+n%k>=k) *phi(k)$ $我们不妨设n=q_1k+r_1 m=q_2k+r$2 $n+m=(q_1+q_2)k+r1+r2$ ${\lfloor}\frac{n+m ...

  5. 342 Power of Four 4的幂

    给定一个整数 (32位有符整数型),请写出一个函数来检验它是否是4的幂.示例:当 num = 16 时 ,返回 true . 当 num = 5时,返回 false.问题进阶:你能不使用循环/递归来解 ...

  6. Python随笔-字符串

    函数title.lower.upper. ct = "hello WORLD" print(ct.title()) #title 以首字母大写的方式显示每个单词 print(ct. ...

  7. hibernate 级联删除报更新失败的问题(org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update)

    首先hibernate级联删除的前提是,首先需要在映射文件中配置,配置多表之间的关联关系: 下面以部门表(Dept)和员工表(Emp)为例: 1.在Emp.hbm.xml映射文件中配置many-to- ...

  8. 数据结构应用实例#栈&单链表#简易计算器

    修改BUG的时候一不小心BUG越修越多,鉴于维护程序并不是学习数据结构的初衷,我已经果断的弃坑了!! 以下内容再不更新,Github上的代码直接无法正常编译运行.... 参考参考就好,学习到栈的作用就 ...

  9. FTP工作原理

    FTP工作原理 FTP两种传输方式:1.ASCII传输2.二进制传输 FTP主被动原理: 主动方式:1.用户与服务器建立控制通道2.客户端发出PORT指令,主动告诉服务器端口号3.服务器主动通过20端 ...

  10. which

    功能说明:显示命令的全路径.   参数选项: -a  默认在PATH路径中由前往后查找命令,如果查找到了,就停止匹配.使用-a选项将遍历所有PATH路径,输出所有匹配项.   参数-a把所有匹配命令路 ...