LinearLayout中的layout_weight属性
布局LinearLayout代码
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="Button1"
- android:background="#008000"
- android:layout_weight="0"
- />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="Button2"
- android:background="#FFFF00"
- android:layout_weight="0"
- />
- </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属性下呈现出正好相反的情况。
分享到:
相关推荐
今天主要说的是对Layout_weight属性的完全解析,以及利用Layout_weight这个属性使用ListView来实现表格的效果,我们都知道Android里面专门有一个TableLayout来实现表格的,说实话,我平常开发中用TableLayout还是...
如果LinearLayout是最外面的一层,它是不会弹出layout_weight属性的, 换句话说最外层不能用layout_weight xml布局如下 <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:...
layout_weight的作用是设置子空间在LinearLayout的重要度(控件的大小比重)。layout_weight的值越低,则控件越重要,下面为大家介绍下具体的使用方法
但是当我们要是图标跟着文字后面,第一时间想到用LinearLayout的layout_weight 来做,但是做出来效果不一样。 后来竟然忘记这个属性了:指定TextView的最大宽度实现自动省略 android:maxWidth=90dp ,加上
用Eclipse加载项目工程 <LinearLayout xmlns:android=... android:layout_weight="0.66" android:background="@drawable/blue_bg" android:orientation="vertical" > android:layout
-<LinearLayout android:background="@drawable/aaa" android:weightSum="1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android=...
-<LinearLayout android:background="@drawable/aaa" android:weightSum="1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android=...
android:layout_weight="1" android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="0dp"> <LinearLayout android:paddingTop="10dp" android:background="#ffffff" android:...
android:layout_weight="1" android:src="@drawable/pbg" android:scaleType="centerCrop"/> android:id="@+id/m3" android:layout_margin= "2dp" android:layout_width="60dp" android:layout_height=...
layout_centerInParent - 将当前元素放置到其容器内的水平方向和垂直方向的中央位置(类似的属性有 :layout_centerHorizontal, layout_alignParentLeft 等) layout_marginLeft - 设置当前元素相对于其容器的左侧...
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”match_parent” android:layout_height=”match_parent” android:orientation=”horizontal”> <...
我用的是HorizontalScrollView来实现水平条的滚动,按钮的背景宽度和图片的一致,以免被拉伸,main.xml的配置如下: <LinearLayout xmlns:android=... android:layout_weight="1" >
废话不多少了,先上布局,布局文件非常简单,一个EditText和一个Button被填充在LinearLayout中其余部分全部留给WebView ``` xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_...
<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="编辑" /> </...
<LinearLayout nandroid:layout_width=“0dp” android:layout_weight u201c1” android:layout_height=“match_parent” android:background> 运行效果如下 因为灰色部分和黑色部分权重相同故两部分的宽度各占总...
android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> android:onClick="scrollTo3" android:background="@android:color/holo_green_light" android:...
android:layout_weight="1"> android:id="@ id/viewpager_default" android:layout_width="match_parent" android:layout_height="match_parent"/> android:id="@ id/indicator_default" ...
android:layout_weight="1"> android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#737373" /> android:layout_width="match_parent" android:layout_height=...
import android.content.ContentResolver; import android.content.Intent; import android.graphics.Bitmap;... android:layout_weight="1" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
为了创建比例大小的子View,可以将LinearLayout的宽度和...对于LinearLayout的几个子View,将它们的宽度都定义为0,android:layout_width=”0dip”,然后使用layout_weight 为每个View指定宽度比例,本例为每个TextVie