`

2011.10.13(4)——— android android:layout_weight

阅读更多
2011.10.13(4)——— android android:layout_weight

参考:http://hi.baidu.com/hbzha/blog/item/8af2b44f9bd8bd1eb2de055b.html


1、
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView
         android:text="red"
         android:background="#aa0000"
         android:layout_width="wrap_content"
         android:layout_height="100dip"/>
     <TextView
         android:text="green"
         android:background="#00aa00"
         android:layout_width="wrap_content"
         android:layout_height="100dip"/>
     <TextView
         android:text="blue"
         android:background="#0000aa"
         android:layout_width="wrap_content"
         android:layout_height="100dip"/>
     <TextView
         android:text="yellow"
         android:background="#aaaa00"
         android:layout_width="wrap_content"
         android:layout_height="100dip"/>
</LinearLayout>


水平布局  textview的宽都是wrap_context

效果如下:





2、

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView
         android:text="red"
         android:background="#aa0000"
         android:layout_width="wrap_content"
         android:layout_height="100dip"
         android:layout_weight="1"/>
     <TextView
         android:text="green"
         android:background="#00aa00"
         android:layout_width="wrap_content"
         android:layout_height="100dip"/>
     <TextView
         android:text="blue"
         android:background="#0000aa"
         android:layout_width="wrap_content"
         android:layout_height="100dip"/>
     <TextView
         android:text="yellow"
         android:background="#aaaa00"
         android:layout_width="wrap_content"
         android:layout_height="100dip"/>
</LinearLayout>


水平布局  textview的宽都是wrap_context 修改red的weight为1

效果如下:





通过1和2 可以得到结论:
在Horizontal的LinearLayout中,控件A和控件B的layout_weight分别设置为2和1,并不代表两者的宽度之比为2:1,2:1针对的是剩余的宽度。 控件的宽度等于空间本身需要的最小宽度,加上剩余宽度中的所占的权重。垂直方向的LinearLayout也同理。


3、

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView
         android:text="red"
         android:background="#aa0000"
         android:layout_width="wrap_content"
         android:layout_height="100dip"
         android:layout_weight="1"/>
     <TextView
         android:text="green"
         android:background="#00aa00"
         android:layout_width="wrap_content"
         android:layout_height="100dip"
         android:layout_weight="1"/>
     <TextView
         android:text="blue"
         android:background="#0000aa"
         android:layout_width="wrap_content"
         android:layout_height="100dip"
         android:layout_weight="1"/>
     <TextView
         android:text="yellow"
         android:background="#aaaa00"
         android:layout_width="wrap_content"
         android:layout_height="100dip"
         android:layout_weight="1"/>
</LinearLayout>


水平布局  textview的宽都是wrap_context 修改所有的textview的weight为1

效果如下:




可以看出来 textview并不是按照1:1:1:1的比例占据宽度的 所以正好验证了上面的结论

4、
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView
         android:text="red"
         android:background="#aa0000"
         android:layout_width="fill_parent"
         android:layout_height="100dip"
         android:layout_weight="1"/>
     <TextView
         android:text="green"
         android:background="#00aa00"
         android:layout_width="wrap_content"
         android:layout_height="100dip"/>
     <TextView
         android:text="blue"
         android:background="#0000aa"
         android:layout_width="wrap_content"
         android:layout_height="100dip"/>
     <TextView
         android:text="yellow"
         android:background="#aaaa00"
         android:layout_width="wrap_content"
         android:layout_height="100dip"/>
</LinearLayout>


水平布局  red的宽为fill_parent 并且weight为1,其他textview的宽都是wrap_context

效果如下:




我们明明设置了red的weight为fill_parent 它却没有填充真个屏幕
所以通过上面的结果 我们可以得到一个信息:

当使用了layout_weight属性时,该属性优先于width和height属性。



5、

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView
         android:text="red"
         android:background="#aa0000"
         android:layout_width="fill_parent"
         android:layout_height="100dip"
         android:layout_weight="1"/>
     <TextView
         android:text="green"
         android:background="#00aa00"
         android:layout_width="wrap_content"
         android:layout_height="100dip"
         android:layout_weight="1"/>
     <TextView
         android:text="blue"
         android:background="#0000aa"
         android:layout_width="wrap_content"
         android:layout_height="100dip"
         android:layout_weight="1"/>
     <TextView
         android:text="yellow"
         android:background="#aaaa00"
         android:layout_width="wrap_content"
         android:layout_height="100dip"
         android:layout_weight="1"/>
</LinearLayout>


水平布局  red的宽为fill_parent 其他textview的宽都是wrap_context 修改所有的textview
的weight为1

效果如下:




这个不明白。。。。
  • 大小: 10.6 KB
  • 大小: 12.9 KB
  • 大小: 12.7 KB
  • 大小: 13.9 KB
  • 大小: 11.6 KB
分享到:
评论
1 楼 pkop57 2011-11-03  
个人认为,weight和fill_parent属性一起使用。。。效果会比较好。设置wrap_content的控制设置weight属性感觉没有用。

相关推荐

    android-autofittextview-master.zip_android_android textview_auto

    android:layout_width= match_parent android:layout_height= wrap_content &gt; &lt;Button android:layout_width= match_parent android:layout_height= wrap_content android:singleLine= true /&gt...

    2011.10.13(2)——— android Matrix学习03

    这篇博客“2011.10.13(2)——— android Matrix学习03”可能深入探讨了Matrix类的一些高级用法和实践技巧。虽然描述中没有提供具体信息,但从标题可以推断,这可能是系列教程的第三部分,继续深化对Matrix的理解。...

    QuarkBrowser_V7.2.5.640_android_pf3300__zh-cn__rel.apk

    QuarkBrowser_V7.2.5.640_android_pf3300__zh-cn__rel.apk

    cap_ffmpeg_impl.hpp

    make #遇到下面错误: make[2]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_ffmpeg.o] Error 1 make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2 ...

    SendSms.zip

    适合android初学者 &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ...

    class_layout_demo.rar_DEMO

    你可以通过`android:orientation`属性设置方向,`android:layout_weight`属性分配子视图的权重,以实现按比例分配空间。 2. **相对布局(RelativeLayout)**:允许视图相对于其他视图的位置进行定位,提供了更灵活...

    Android开发—使用ActivityGroup来切换Activity和Layout

    android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"&gt; android:background="@android:color/black" android:layout_gravity="bottom" android:...

    基于因子分析的我国A股上市...争力评价——以医药企业为例_张澳.caj

    基于因子分析的我国A股上市...争力评价——以医药企业为例_张澳.caj

    Android 控件说明

    android:layout_marginLeft="10dp" android:layout_alignParentTop="true" /&gt; ``` #### 2\. 布局属性详解 除了位置属性,还有一些基本的布局属性: - `layout_width` 和 `layout_height`:用于定义视图的宽度...

    taobao4android_703304.apk

    taobao4android_703304.apk

    Android代码(相对布局).zip

    - 使用`android:layout_weight`属性实现弹性布局效果,让视图根据需要自动调整大小。 - 使用`&lt;merge&gt;`标签减少布局层次,优化性能。 7. **总结**: "Android代码(相对布局).zip"中的"Pro07(相对布局)"项目...

    abdroid relativelayout属性

    4. `android:layout_alignParentBottom`:视图的下边缘贴紧父视图的下边缘。 5. `android:layout_alignParentLeft`:视图的左边缘贴紧父视图的左边缘。 6. `android:layout_alignParentRight`:视图的右边缘贴紧父...

    Android 线性布局 实例

    1. `android:layout_width`和`android:layout_height`:同样用于子视图,但可以结合`android:layout_weight`使用,以实现按比例分配空间。 2. `android:layout_weight`:为子视图分配额外的空间,根据权重比例分配。...

    Android多行多列的单选按钮组的实现

    android:layout_weight="1" android:orientation="horizontal"&gt; &lt;!-- 第一行的RadioButton们 --&gt; android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/gray_...

    Android 选择图片、上传图片之PictureSelector

    集成方式 implementation ...build.gradle加入 allprojects {  repositories {  jcenter() ... } }   ...PictureSelector适配已经做的很好了,功能齐全,且兼容性好 1.适配android6.0+系统 ...4.动态获取系统

    Hibernate_QBC案例.rar

    // 1.查询所有 // 2.条件查询 // 3.模糊查询 // 4.范围查询 // 5.排序查询(order by) // 6....// 7....// 8....———————————————— 原文链接:https://blog.csdn.net/qq_29001539/article/details/105182223

    jisuanqi.rar_android_android 表格_jisuanqi.app

    在Android开发中,创建一个功能丰富的计算器应用是一个常见的任务,涉及到UI设计、事件处理和基本的数学运算。本文将深入探讨如何在Android平台上利用表格布局(TableLayout)来实现一个计算器,包括加减乘除等基本...

    一款非常绚丽的进度条

    Android 加载动画(关于SpinKit的使用) &lt;com.github.ybq.android.spinkit.SpinKitView xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/spin_kit" android:layout_width="wrap_...

    qt窗口部件——其他窗口部件代码

    ———————————————— 版权声明:本文为CSDN博主「MechMaster」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:...

    Android Studio多个按钮跳转多个页面,利用选项卡功能实现

    在Android应用开发中,Android Studio是Google官方推荐的集成开发环境(IDE)。本教程将详细介绍如何在Android Studio中实现一个功能,即通过多个按钮跳转到多个不同的页面,并利用选项卡功能来增强用户体验。首先,...

Global site tag (gtag.js) - Google Analytics