`

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-extend-1.0.5.aar依赖库com.guo.android_extend

    虹软ArcSoft 人脸识别AndroidDemo中有一个Lib通过公网始终无法下载,替换进去就好了。 依赖替换为 implementation files('libs/android-extend-1.0.5.aar')。 依赖库com.guo.android_extend

    2011.10.12(3)——— android Matrix学习02

    这篇博客“2011.10.12(3)——— android Matrix学习02”可能深入探讨了Matrix类的使用方法,虽然具体的细节没有给出,但我们可以根据Matrix的基本功能和常见用法来展开讨论。 1. **Matrix类的介绍**:Matrix是...

    .android_system_config.prop

    .android_system_config.prop

    realtek_wifi_SDK_for_android_JB.tar.gz_RTL8188_RTL8189_android a

    Realtek公司作为全球知名的半导体制造商,为满足这一需求,推出了专门针对Android系统的WiFi驱动软件开发套件(SDK)——"realtek_wifi_SDK_for_android_JB.tar.gz_RTL8188_RTL8189_android a"。这款SDK主要针对...

    2011.10.19——— android 显示一行内容并录制其音频

    这篇博客“2011.10.19——— android 显示一行内容并录制其音频”可能详细探讨了如何实现这个功能。虽然描述部分没有提供具体信息,但我们可以基于标签“源码”和“工具”来推测文章内容,以及从文件名...

    基于WXSS_WXML技术...序的开发——以西岭雪山为例_邹明荣.caj

    基于WXSS_WXML技术...序的开发——以西岭雪山为例_邹明荣.caj

    2011.10.09——— android ImageView放大缩小(2)

    标题中的“2011.10.09——— android ImageView放大缩小(2)”指的是一个关于Android平台中ImageView组件的优化技术,特别是如何处理图片的缩放问题。在Android应用开发中,ImageView是用于显示图像的常见组件,但...

    NPSWF32调试版本.rar

    ▕ ▕ ▕—————————————————————————————————————— ▕ ▕——[13.27MB] 【NPSWF32调试版本_11.1.102.55_debug】 ▕ ▕ ▕——[856 B] flashplayer.xpt ▕ ▕ ▕——[3.90MB...

    2011.09.27(2)——— android ImageView上下同时拉伸的效果

    这篇2011年的博客文章"2011.09.27(2)——— android ImageView上下同时拉伸的效果"探讨了如何实现一个自定义的ImageView,使得图像在垂直方向上能够同时在顶部和底部进行拉伸,以达到特定的视觉效果。这种效果通常...

    Android Layout样式布局

    ### Android Layout样式布局详解 #### 一、概述 在Android应用开发中,界面设计是非常重要的一环,而界面设计的核心就是布局(Layout)。布局决定了应用界面的结构与外观,是用户体验好坏的重要因素之一。本文将...

    Android 控件说明

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

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

    android布局+控件

    ### Android布局与控件知识点详解 #### 一、布局类型概览 在Android开发中,布局是构建用户界面的基础。合理的布局设计不仅能够提升用户体验,还能有效管理界面元素的位置和大小,使应用适应不同尺寸和分辨率的...

    android使用CollapsingToolbarLayout实现折叠效果

    4. 设置折叠行为:通过`app:layout_scrollFlags`属性控制子视图的滚动行为。`scroll`表示视图会随着内容滚动而移动,`enterAlways`和`enterAlwaysCollapsed`会在向上滚动时总是显示或始终折叠。 5. 添加内容视图:...

    计算机视觉大作业-三维重建.zip

    计算机视觉大作业——三维重建.zip计算机视觉大作业——三维重建.zip计算机视觉大作业——三维重建.zip计算机视觉大作业——三维重建.zip计算机视觉大作业——三维重建.zip计算机视觉大作业——三维重建.zip计算机...

    android XML文件详解

    - `android:layout_alignParentBottom`, `android:layout_alignParentLeft`, `android:layout_alignParentRight`, `android:layout_alignParentTop` - 这些属性用于指定视图是否应该与父容器的某个边缘对齐。 - ...

    local_policy.jar和US_export_policy.jar

    Java Cryptography Extension (JCE) 是Java平台标准的一部分,它提供了高级加密算法和技术,使得开发者能够在Java应用程序中实现安全的加密功能。在Java 1.6、1.7和1.8版本中,JCE包括两个重要的jar文件:`local_...

    MT6575_Android_scatter.txt

    cpu修改代码,联想A60+的Android_scatter 用于升级手机cpu,功能很大,结构小巧,但很使用

    线性布局的使用

    - **解释**:在这个例子中,如果`TextView1`设置了`android:layout_weight="1"`,而其他`TextView`没有设置权重,则`TextView1`将会占据剩余空间的一部分,其余的`TextView`则保持默认大小。如果所有`TextView`都...

    目前Android恶意软件分类

    在2013年2月28日之前,Android平台已经遭遇了大量恶意软件的威胁,这些恶意软件家族被详细记录并分类,以帮助用户和开发者了解和防范。下面将详细阐述这些恶意软件的特点和行为。 首先,恶意软件的分类包括但不限于...

Global site tag (gtag.js) - Google Analytics