`
dowhathowtodo
  • 浏览: 777446 次
文章分类
社区版块
存档分类
最新评论

android布局之LinearLayout, layout_weight

 
阅读更多
layout_weight这个参数很有用,在LinearLayout布局中可以通过这个调整各个组件占的面积的权重,如下是一个例子

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3. <LinearLayout>
  4. 线性布局配置,在这个标签中,所有的组件都是按由上到下的排列
  5. -->
  6. <LinearLayout
  7. xmlns:android="http://schemas.android.com/apk/res/android"
  8. android:orientation="vertical"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent">
  11. <!-- android:orientation="vertical" 表示竖直方向排列
  12. android:orientation="horizontal"表示水平方向排列
  13. android:layout_width="fill_parent"定义当前视图在屏幕上
  14. 可以消费的宽度,fill_parent即填充整个屏幕的宽度。
  15. android:layout_height="wrap_content":随着文字栏高度的不同
  16. 而改变这个视图的高度。有点自动设置框度或者高度的意思
  17. layout_weight 用于给一个线性布局中的诸多视图的重要度赋值。
  18. 所有的视图都有一个layout_weight值,默认为零,意思是需要显示
  19. 多大的视图就占据多大的屏幕空 间。若赋一个高于零的值,则将父视
  20. 图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight
  21. 值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布
  22. 局的layout_weight值中所占的比率而定。
  23. 举个例子:比如说我们在 水平方向上有一个文本标签和两个文本编辑元素。
  24. 该文本标签并无指定layout_weight值,默认就为0,所以它将占据最少的空间。
  25. 如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分
  26. 在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个
  27. 文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2,
  28. 则剩余空间的三分之二分给第一个,三分之一分给第二个(数值越小,重要
  29. 度越高)。
  30. -->
  31. <LinearLayout
  32. android:orientation="horizontal"
  33. android:layout_width="fill_parent"
  34. android:layout_height="fill_parent"
  35. android:layout_weight="1">
  36. <TextView
  37. android:text="red"
  38. android:gravity="center_horizontal"
  39. android:background="#aa0000"
  40. android:layout_width="wrap_content"
  41. android:layout_height="fill_parent"
  42. android:layout_weight="1"/>
  43. <TextView
  44. android:text="green"
  45. android:gravity="center_horizontal"
  46. android:background="#00aa00"
  47. android:layout_width="wrap_content"
  48. android:layout_height="fill_parent"
  49. android:layout_weight="1"/>
  50. <TextView
  51. android:text="blue"
  52. android:gravity="center_horizontal"
  53. android:background="#0000aa"
  54. android:layout_width="wrap_content"
  55. android:layout_height="fill_parent"
  56. android:layout_weight="1"/>
  57. <TextView
  58. android:text="yellow"
  59. android:gravity="center_horizontal"
  60. android:background="#aaaa00"
  61. android:layout_width="wrap_content"
  62. android:layout_height="fill_parent"
  63. android:layout_weight="1"/>
  64. </LinearLayout>
  65. <LinearLayout
  66. android:orientation="vertical"
  67. android:layout_width="fill_parent"
  68. android:layout_height="fill_parent"
  69. android:layout_weight="2">
  70. <TextView
  71. android:text="row one"
  72. android:textSize="15pt"
  73. android:layout_width="fill_parent"
  74. android:layout_height="wrap_content"
  75. android:layout_weight="1"/>
  76. <TextView
  77. android:text="row two"
  78. android:textSize="15pt"
  79. android:layout_width="fill_parent"
  80. android:layout_height="wrap_content"
  81. android:layout_weight="1"/>
  82. <TextView
  83. android:text="row three"
  84. android:textSize="15pt"
  85. android:layout_width="fill_parent"
  86. android:layout_height="wrap_content"
  87. android:layout_weight="1"/>
  88. <TextView
  89. android:text="row four"
  90. android:textSize="15pt"
  91. android:layout_width="fill_parent"
  92. android:layout_height="wrap_content"
  93. android:layout_weight="1"/>
  94. </LinearLayout>
  95. </LinearLayout>

  1. package com.eoembile.iceskysl.testlayout;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. public class Views extends Activity {
  5. /** Called when the activity is first created. */
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10. }
  11. }

分享到:
评论

相关推荐

    Android App中的多个LinearLayout嵌套布局实例解析

    在做android UI布局时,用了LinearLayout嵌套,发现效果并不如我预料一般 查了下资料,说是要设置layout_weight属性 资料说得不是很清楚,也没仔细看,就去弄,结果越弄越混乱。 于是静下心来,自己写xml测试,发现...

    Android应用中通过Layout_weight属性用ListView实现表格

    今天主要说的是对Layout_weight属性的完全解析,以及利用Layout_weight这个属性使用ListView来实现表格的效果,我们都知道Android里面专门有一个TableLayout来实现表格的,说实话,我平常开发中用TableLayout还是...

    Android控件大全以及各布局空间的使用方式

    android:layout_weight="1" android:layout_height="wrap_content" android:text="行1列1" /&gt; &lt;TextView android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content...

    Android布局之LinearLayout线性布局

    LinearLayout是线性布局控件:要么横向排布,要么竖向排布 常用属性: android:gravity————设置的是控件自身上面的内容位置 android:layout_gravity—–设置控件本身相对于父控件的显示位置 android:layout_...

    WebViewDownloadTest.zip

    废话不多少了,先上布局,布局文件非常简单,一个EditText和一个Button被填充在LinearLayout中其余部分全部留给WebView ``` &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:...

    Android实现图片自动轮换

    package xatu.cn.androidtest_one; import android.content.ContentResolver; import android.content.Intent;... android:layout_weight="1" /&gt; &lt;/LinearLayout&gt; &lt;/android.support.constraint.ConstraintLayout&gt;

    高仿微信界面

    android:layout_weight="1"&gt; &lt;/android.support.v4.view.ViewPager&gt; android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#737373" /&gt; android:layout_width=...

    android layout 按比例布局的代码

    为了创建比例大小的子View,可以将LinearLayout的宽度和...对于LinearLayout的几个子View,将它们的宽度都定义为0,android:layout_width=”0dip”,然后使用layout_weight 为每个View指定宽度比例,本例为每个TextVie

    SwipeListViewTest项目

    &lt;Button android:id="@+id/example_row_b_action_2" android:layout_width="0dp" android:layout_height="60dp" android:layout_gravity="center" android:layout_marginLeft="10dp" android:layout_weight="1" ...

    wkp111_StickLayout-粘性控件,其任意一个子控件都可滑动停留,本质为NestedScrollView和LinearLayout的结合。.zip

     android:layout_weight="1"  android:layout_width="0dp"  android:layout_height="match_parent"/&gt;    android:onClick="scrollTo3"  android:background="@android:color/holo_green_light"  android:...

    【Android Studio代码】线性布局

    用Android Studio 2.3.3做的线性布局例子,主要用到的标签有:&lt;LinearLayout&gt;&lt;/LinearLayout&gt;;主要用到的设置有:android:orientation="horizontal"、android:layout_weight="1"。

    linearLayout线性布局

    &lt;LinearLayout nandroid:layout_width=“0dp” android:layout_weight u201c1” android:layout_height=“match_parent” android:background&gt; 运行效果如下 因为灰色部分和黑色部分权重相同故两部分的宽度各占总...

    CircleIndicator-一个轻量级的viewpager指示器 ,类似于nexus5 启动器的效果.zip

     android:layout_weight="1"&gt;  &lt;android.support.v4.view.ViewPager  android:id="@ id/viewpager_default"  android:layout_width="match_parent"  android:layout_height="match_parent"/&gt;    ...

    Android计算器实例

    一个Android计算器实例,布局文件中用到了Linearlayout、TableLayout、android:layout_weight的嵌套

    Android注册登录界布局设计

    1、selector的使用 2、style的使用 3、将一个TextView放在一个EditText中(使用相对布局…… ...6、是两个控件在同一行平均分布(这两个控件放在一个LinearLayout中,分别设置2控件的属性android:layout_weight="1

    LCRapidDevelop-master

    android:layout_weight="1" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:layout_marginRight="10dp...

    详解Android中weight的使用方法

    android中对weight的学习可以说是必须的,如果UI布局仅仅使用dp与sp等等,会让布局显得极度不灵活,毕竟各个手机屏幕大小不同,更别说是还有ipad之类的了,所以也是同做本人近期做的一个小UI来分享一下weight的使用...

    三联生活周刊应用布局效果.zip

    这个是三联生活周刊应用布局效果的源码,android自定义viewgroup实现等分格子布局,实现这样的效果:一般的思路就是,直接写布局文件,用LinearLayout 嵌套多层子LinearLayout,然后根据权重layout_weight可以达到上面...

Global site tag (gtag.js) - Google Analytics