`

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属性感觉没有用。

相关推荐

    WeCom_android_4.1.8.23932_arm64_100038.apk

    WeCom_android_4.1.8.23932_arm64_100038.apk

    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的理解。...

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

    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`:用于定义视图的宽度...

    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`:为子视图分配额外的空间,根据权重比例分配。...

    JDK_1.7,amd64_ubuntu,安装包,直接下载安装即可完成1.7版本的SDK包。原生安装,不用配置环境变量,

    文件说明: 1,安装说明.txt ——————————安装手册 2,jdk-170.tar.gz ————...4,check_java.sh———————————版本核对 注意:使用命令接口切换时,需要将自己配置的环境变量注释或者删掉!!!

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

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

    Keil.STM32F4xx_DFP.2.15.0.pack(STM32F4xx系列固件库for Keil MDK 5)

    Version: 2.15.0 (2020-09-28) Keil.STM32F4xx_DFP.2.15.0.pack Download Updated Pack to STM32Cube_FW_F4 Firmware Package version V1.25.1 using HAL Drivers V1.7.9. STM32CubeMX integration (Version 6.0.1)...

    iperf_android.apk.zip

    `iperf_android.apk` 是专为Android设备优化的iPerf3版本,用户可以通过安装此APK在手机或平板上进行网络性能测试。以下是一些应用场景: 1. **网络诊断**:当用户感觉网络慢或者不稳定时,可以用iPerf3测试并定位...

    Andriod接收广播的类

    'android.server.checkin.FOTA_RESTART' 通过 OTA 下载并安装操作系统更新。 'android.server.checkin.FOTA_UPDATE' 用户按下了'Media Button'。 'android.intent.action.MEDIABUTTON' 扩展介质(扩展卡)已经从 ...

    win 10,adb 驱动程序, android_winusb

    标题中的“win 10,adb 驱动程序, android_winusb”指的是在Windows 10操作系统上安装和使用Android Debug Bridge(ADB)时所需的驱动程序问题。ADB是Android开发者用于与设备进行通信的强大工具,它允许用户执行...

    com.google.android.webview.apk

    webview全称叫做Android System WebView,它是Android生态系统的重要组成部分,也是Chrome浏览器的内核。可以让你的手机变得瞬间畅通,它会及时优化网页相关数据,有着接入式的操作体验数据,各种类型的应用程序可...

    Android五种布局模式

    在Android开发中,布局(Layout)是构建用户界面的关键元素,它定义了屏幕上各个组件的排列方式和相互关系。本文将深入探讨Android的五种主要布局模式:线性布局(LinearLayout)、相对布局(RelativeLayout)、帧...

Global site tag (gtag.js) - Google Analytics