`
wsql
  • 浏览: 11812102 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

LinearLayout中的layout_weight属性

 
阅读更多

LinearLayout中的layout_weight属性

布局LinearLayout代码

Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <Button
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:text="Button1"
  11. android:background="#008000"
  12. android:layout_weight="0"
  13. />
  14. <Button
  15. android:layout_width="fill_parent"
  16. android:layout_height="fill_parent"
  17. android:text="Button2"
  18. android:background="#FFFF00"
  19. android:layout_weight="0"
  20. />
  21. </LinearLayout>

layout_weight属性如果没有设置,则默认为0.

按钮1:button1,绿色

按钮2:button2,黄色

下面以按钮1和按钮2的weight属性(layout_weight)的变化,分别进行记录。

这里测试的是垂直布局android:orientation="vertical",所以针对组件的android:layout_height属性;如果测试的水平布局,则需针对组件的android:layout_width属性。

(一) android:layout_height="fill_parent"

button1(weight=0) button2(weight=0) button1(weight=0) button2(weight=1)




button1(weight=1) button2(weight=1)

button1占1/2,button2占1/2

button1(weight=1) button2(weight=2)

button1占2/3,button2占1/3





button1(weight=1) button2(weight=9)

button1占9/10,button2占1/10

button1(weight=1) button2(weight=30)

button1占30/31,button2占1/31

(二) android:layout_height="wrap_content"





button1(weight=0) button2(weight=0)

button1(weight=0) button2(weight=1)




button1(weight=1) button2(weight=1)

button1占1/2,button2占1/2

button1(weight=1) button2(weight=2)

button1占1/3,button2占2/3





button1(weight=1) button2(weight=9)

button1(weight=1) button2(weight=30)

通过以上实验可知:

(一) android:layout_height="fill_parent"

(二) android:layout_height="wrap_content"

在(一)、(二)2种情况在相同的weight属性下呈现出正好相反的情况。


分享到:
评论

相关推荐

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

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

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

    如果LinearLayout是最外面的一层,它是不会弹出layout_weight属性的, 换句话说最外层不能用layout_weight xml布局如下 &lt;LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:...

    Android中的android:layout_weight使用详解

    layout_weight的作用是设置子空间在LinearLayout的重要度(控件的大小比重)。layout_weight的值越低,则控件越重要,下面为大家介绍下具体的使用方法

    Android中Textview和图片同行显示(文字超出用省略号,图片自动靠右边)

    但是当我们要是图标跟着文字后面,第一时间想到用LinearLayout的layout_weight 来做,但是做出来效果不一样。 后来竟然忘记这个属性了:指定TextView的最大宽度实现自动省略 android:maxWidth=90dp ,加上

    android_QQ_例子

    用Eclipse加载项目工程 &lt;LinearLayout xmlns:android=... android:layout_weight="0.66" android:background="@drawable/blue_bg" android:orientation="vertical" &gt; android:layout

    Android实训购物车页面

    -&lt;LinearLayout android:background="@drawable/aaa" android:weightSum="1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android=...

    Android购物车代码

    -&lt;LinearLayout android:background="@drawable/aaa" android:weightSum="1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android=...

    WeChatSample

    android:layout_weight="1" android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="0dp"&gt; &lt;LinearLayout android:paddingTop="10dp" android:background="#ffffff" android:...

    九宫格牌翻转游戏demo

    android:layout_weight="1" android:src="@drawable/pbg" android:scaleType="centerCrop"/&gt; android:id="@+id/m3" android:layout_margin= "2dp" android:layout_width="60dp" android:layout_height=...

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

    layout_centerInParent - 将当前元素放置到其容器内的水平方向和垂直方向的中央位置(类似的属性有 :layout_centerHorizontal, layout_alignParentLeft 等) layout_marginLeft - 设置当前元素相对于其容器的左侧...

    Android layout_weight使用方法及实例

    &lt;LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”match_parent” android:layout_height=”match_parent” android:orientation=”horizontal”&gt; &lt;...

    android水平导航条的实现

    我用的是HorizontalScrollView来实现水平条的滚动,按钮的背景宽度和图片的一致,以免被拉伸,main.xml的配置如下: &lt;LinearLayout xmlns:android=... android:layout_weight="1" &gt;

    WebViewDownloadTest.zip

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

    SwipeListViewTest项目

    &lt;Button android:id="@+id/example_row_b_action_3" android:layout_width="0dp" android:layout_height="60dp" android:layout_gravity="center" android:layout_weight="1" android:text="编辑" /&gt; &lt;/...

    linearLayout线性布局

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

    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:...

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

     android:layout_weight="1"&gt;    android:id="@ id/viewpager_default"  android:layout_width="match_parent"  android:layout_height="match_parent"/&gt;    android:id="@ id/indicator_default" ...

    高仿微信界面

    android:layout_weight="1"&gt; android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#737373" /&gt; android:layout_width="match_parent" android:layout_height=...

    Android实现图片自动轮换

    import android.content.ContentResolver; import android.content.Intent; import android.graphics.Bitmap;... android:layout_weight="1" /&gt; &lt;/LinearLayout&gt; &lt;/android.support.constraint.ConstraintLayout&gt;

    android layout 按比例布局的代码

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

Global site tag (gtag.js) - Google Analytics