Android layout ----LinearLayout小记

一种android 的布局方式,在其中的所有的控件都是一个一个的排列下去的。

  

 <LinearLayout

     android:orientation="horizontal"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent"

     >

//下面可以有多个控件

//…………………………………….

/每一个都可以有android:layout_weight属性的设置

</LinearLayout>

 

如上如果一个activity中只有一个LinearLayout其他的控件在他的下面进行一系列的排列,

 

android:orientation="horizontal"   ///水平的一个个的排列

android:orientation=" vertical"   ///垂直的一个个的排列

 

可以指定排列的方式;

 

例子二:

<LinearLayout

     android:orientation="horizontal"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent"

     android:layout_weight="1">

      <TextView

         android:text="红色"

        指定text文本的显示的位置

          android:gravity="center_horizontal|center_vertical" 

         android:background="#aa0000"

         android:layout_width="fill_parent"

         android:layout_height="fill_parent"

         android:layout_weight="1"/> 

///每个控件的layout_weight可以均分LinearLayout的展示比例

      <TextView

         android:text="绿色"

         android:gravity="center_horizontal"

         android:background="#00aa00"

         android:layout_width="fill_parent"

         android:layout_height="fill_parent"

         android:layout_weight="1"/>

      <TextView

         android:text="蓝色"

         android:gravity="center|bottom"

         android:background="#0000aa"

         android:layout_width="fill_parent"

         android:layout_height="fill_parent"

         android:layout_weight="1"/>

      <TextView

         android:text="黄色"

         android:gravity="bottom"

         android:background="#aaaa00"

         android:layout_width="fill_parent"

         android:layout_height="fill_parent"

         android:layout_weight="1"/>

  </LinearLayout>

 

 

<!-- android默认的就是LinearLayout类型的,垂直排列-->

<!--

android:gravity

该属性用于控制布局中控件的对齐方式。如果是没有子控件的控件设置此属性,

表示其内容的对齐方式,比如说TextView里面文字的对齐方式;

若是有子控件的控件设置此属性,则表示其子控件的对齐方式

 -->

 

 

值得注意的是:

如果在一个总布局是LinearLayout的Activity中,如果存在俩个子LinearLayout,作为子控件存在,对于android:layout_weight="1" 的指定是相反的,即是说:如果第一个指定了=2,第二个指定了=1,则第一个占有1/3的比例


=======================================================================================

下面是在网上找的:

在网上看了一些对Layout_weight的讲解,有些说的比较片面,只列举了一种情况,然后自己通过实验和一些比较好的文章总结了一下,特此记录下来,以备以后所用。Layout_weight是线性布局,也就是LinearLayout里面用到的,下面通过实验来看这个Layout_weight的特性。

1.当控件的属性android:layout_width="fill_parent"时,布局文件如下:

 

Xml代码  复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <Button android:layout_width="fill_parent"
  6. android:layout_height="wrap_content" android:layout_weight="1"
  7. android:text="Button1" />
  8. <Button android:layout_width="fill_parent"
  9. android:layout_height="wrap_content" android:layout_weight="2"
  10. android:text="Button2" />
  11. </LinearLayout>

在这里Button1的Layout_weight=1,Buttong2的Layout_weight=2,运行效果为:


我们看到,Button1占了2/3,Button2占了1/3。如果此时把button2的weight设置成2000,不是说Button2就消失了,而是Button1的宽度几乎占满了屏幕宽度,而屏幕最后一丝细条则是留给Button2的,已近非常小了,没有显示出来。截图如下:





得出结论在layout_width设置为fill_parent的时候,layout_weight代表的是你的控件要优先尽可能的大,但尽可能大是有限度的,即fill_parent.

 

2.当控件的属性android:layout_width="wrap_content"时,布局文件如下:

 

Xml代码  复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <Button android:layout_width="wrap_content"
  6. android:layout_height="wrap_content" android:layout_weight="1"
  7. android:text="Button1" />
  8. <Button android:layout_width="wrap_content"
  9. android:layout_height="wrap_content" android:layout_weight="2"
  10. android:text="Button2" />
  11. </LinearLayout>

同样,Button1的weight设置为1,Button2的weight设置为2,但是效果与fill_parent的效果截然相反。运行效果如下:



这时,和fill_parent正好相反,Button1的宽度占据了屏幕宽度的1/3,而Button2的宽度占据了屏幕的2/3,如果此时把Button1的weight设置为2000,按照之前理解,Button1应该小的几乎在屏幕上看不到,但是错了,实验告诉我们,当Button1的weight非常小时,也要"wrap_content",也就是要保证Button1至少能够显示。以下是Button1设置weight为2000时的运行截图:



我们看到,Button1已经足够小,但是要保证他能显示出来,因此得出结论:

在layout_width设置为wrap_content的时候,layout_weight代表的是你的控件要优先尽可能的小,但这个小是有限度的,即wrap_content.

当了解这些后,我们再设计程序时,为了能够自适应屏幕,不想给控件一个指定的宽度和高度,就可以使用这个weight属性来让它按自己比例来划分屏幕高度或者宽度了。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值