通常使用权重时,layout_wight layout_height 一般设置为0. 这里讲一下宽属性设置为warp_content,再结合权重使用。

权重尺寸的计算方式

android:layout_weight的真实含义是: 那么该 View的宽度 等于 原有宽度(android:layout_width)加上剩余空间的占比!

剩余空间 = 父控件空间 - 所有子控件所需要的空间

示例一 : wight 和 match_parent 使用

设屏幕宽度为w,  两个TextView的宽度设置为match_parent, 那么它们两个view测量申请的宽度均为屏幕宽度w.

剩余宽度 =  父控件空间 - 所有子控件所需要的空间 = w - ( w + w) =  -w;

第一个TextView剩余宽度 = -w * 1/3 = -1/3 w

第一个TextView宽度 = 原有宽度(android:layout_width) + 剩余空间的占比 = w + (-w * 1/3)  = 2/3 w

第二个TextView宽度 = 原有宽度(android:layout_width) + 剩余空间的占比 = w + (-w * 2/3)  = 1/3 w

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:background="#fff"android:text="  111"android:textSize="15dp" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="2"android:background="#0a1"android:text="222"android:textSize="15dp" /></LinearLayout>

示例二: wight 和 wrap_content使用

两个TextView文本内容都是1234567 , 他们测量所需要的绘制宽度为 182px,  父控件总共是300px。

剩余宽度 = 父控件空间 - 所有子控件所需要的空间 = 300px - 182px * 2 = -64px

第一个TextView宽度 = 原有宽度(android:layout_width) + 剩余空间的占比 = 182px +  (-64px * 1/3) = 160px

第二个TextView宽度 = 原有宽度(android:layout_width) + 剩余空间的占比 = 182px +  (-64px * 2/3) = 140px

<LinearLayoutandroid:layout_width="300px"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:autoSizeTextType="none"android:background="#fff"android:lines="1"android:text="1234567"android:textSize="15dp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="2"android:autoSizeTextType="none"android:background="#0a1"android:lines="1"android:text="1234567"android:textSize="15dp" />
</LinearLayout>

实践一:wight 和 wrap_content使用  平分剩余空间

如下图,一个列表,每个item水平分布两个textview, 字体内容不确定。要求字体只显示一行,且必须显示完整。在内容显示不下时,缩小字体。

备注:如下解决方案可能并不是最优的解决方案,可以通过代码,动态计算每一条item中文案绘制所需要的宽度,调整字体大小,让左右两侧字体大小一致且能让内容完整显示。

<LinearLayoutandroid:layout_width="90dp"android:layout_height="wrap_content"android:orientation="horizontal"><androidx.appcompat.widget.AppCompatTextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:autoSizeMinTextSize="3dp"android:autoSizeTextType="uniform"android:background="@android:color/white"android:gravity="center_vertical"android:lines="1"android:text="涨跌幅:"android:textSize="15dp" /><androidx.appcompat.widget.AppCompatTextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_weight="1"android:autoSizeMinTextSize="3dp"android:autoSizeTextType="uniform"android:background="@android:color/holo_green_light"android:lines="1"android:text="32.32%"android:textSize="15dp" /></LinearLayout>
。。。。。

实践二: wight 和 wrap_content minHeight 共同使用,补全不确定剩余空间。

需求:如下图所示,右侧图片宽高尺寸固定,宽93dp 高80dp , 左侧第一行是文本,默认情况下最多五行,可点击展开与折叠,第二行是投顾锦囊。在左侧文本内容较少时,左侧控件整体高度如下图,投顾锦囊控件要与ImageView对齐,第一行文本上方始终与图片上方对齐。

解决左侧最小高度问题:设置左侧left_root_View android:minHeight="85dp".  因为要去掉TextView默认下上padding,与ImageView实现绝对对齐(可怕的UI设计)。。。, 85dp = 80dp + 3dp + 2dp ;  然后为了保证投顾锦囊在最小高度下与图片对齐,在第一行文本控件与投顾锦囊父控件中间加一个 View,设置高度为1dp, wight=1, 这样就可以实现了。

实现原理:left_root_View 控件设置了最小高度 minHeight 为85dp, 它的高度属性为wrap_content属性, 当测量的子view之后,测量结果小于minHeight则按minHeight有设置高度,如果大于minHeight则按实际来设置; 假设left_root_view中三个子控件中,第一控件测量需要高度40dp,第二控件测量所需要高度为1dp, 第三控件所需高度为20dp, 此时所需要的总高度 61dp。 由于第二个控件设置了权重,且只有它一个控件有权重。在第一次测量完成后,根据结果,left_root_view的高度为minHeight, 然后再根据权重分配原则,根据第二控件的父控件 left_root_view 的实际尺寸 - 子view所需尺寸 = 85 - 61 = 24dp = 剩余尺寸,剩余尺寸会根据权重比分配到设置权重的控件上。此处只有第二个控件设置了权重,剩余尺寸全部分配给它,那么它的尺寸为 1dp + 24dp = 25dp.   如果三个子控件所需高度超过minHeight,假设第一、三控件所需100dp、20dp的高度,此时测量三个子控件所需高度为100dp + 1dp + 20dp = 121dp > minHeight,  剩余尺寸 = 121dp - 121dp = 0dp , 第二控件的高度为1dp + 0dp = 1dp.

备注:第二个控件的高度不能设置为0dp, 因为如果设置为0dp和权重,且它的父控件的高度属性为wrap_content, 那么它的测量模式为MeasureSpec.UNSPECIFIED,  这样会让父控件获取最大的高度空间来分配给它,如果这是一条item,那么item的高度可能就是列表的最大高度, 第二控件的高度 = 列表高度 - 第1 - 第3控件测量的高度。所以这里不能将第二控件的高度设置为0。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="13dp"><LinearLayoutandroid:id="@+id/left_root_View"android:layout_width="0dp"android:background="#88999999"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_toLeftOf="@+id/imageView"android:layout_weight="1"android:minHeight="85dp"android:orientation="vertical"><TextViewandroid:id="@+id/titleTv"android:background="#88daadaa"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ellipsize="end"android:maxLines="5"android:textColor="@color/black"android:textSize="15dp"tools:text="短线狙击  \n股票短线狙击  \n股票短线狙击  \n股票短线狙击  \n股票 " /><Viewandroid:layout_width="40dp"android:layout_height="1dp"android:layout_weight="1"android:background="#3f0" /><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#999"><TextViewandroid:id="@+id/sourceTv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:gravity="center_vertical"android:text="投顾锦囊"android:textSize="13dp" /><TextViewandroid:id="@+id/timeTv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:layout_marginLeft="5dp"android:ellipsize="end"android:gravity="center_vertical|right"android:lines="1"tools:text="2022.1.21" /></RelativeLayout></LinearLayout><ImageViewandroid:id="@+id/picIv"android:layout_width="93dp"android:layout_height="80dp"android:layout_marginLeft="10dp"android:layout_marginTop="3dp"android:layout_marginBottom="2dp"android:background="#fa1"android:src="@drawable/welfare_mall" /></LinearLayout>

android:layout_weight权重与warp_content配合使用相关推荐

  1. Android LinearLayout的android:layout_weight属性

    本文主要介绍Android LinearLayout的android:layout_weight属性意义 android:layout_weight为大小权重,相当于在页面上显示的百分比,它的计算是根 ...

  2. android:layout_weight的真实含义

    首先声明只有在Linearlayout中,该属性才有效.之所以android:layout_weight会引起争议,是因为在设置该属性的同时,设置android:layout_width为wrap_c ...

  3. android 布局权重问题(最近布局经常坑爹)

    android 布局 权重 With layout_weight you can specify a size ratio between multiple views. E.g. you have ...

  4. android:layout_weight属性的简单使用

    效果: style.xml <style name="etStyle2"><item name="android:layout_width"& ...

  5. 情况控件Android layout_weight用法图解

    时间紧张,先记一笔,后续优化与完善. 直接上代码和图片. 情况一: <?xml version="1.0" encoding="utf-8"?> & ...

  6. Android 布局属性 Android:layout_weight 总结

    Android 为我们提供了五种布局方式 线性布局:LinerLayout 表格布局:TableLayout 相对布局:RelativeLayout 绝对布局:AbsoluteLayout 帧布局:F ...

  7. Android字体权重,android – 如何设置自定义字体权重?

    就像主题暗示的那样,我想以编程方式更改/设置任意字体的font weight. 编辑:我的意思是一般用于绘制字符串的字体. 谢谢你的任何信息. 解决方法: 据我所知,加载字体后无法修改.您可以从现有字 ...

  8. Android中Fragment+ViewPager的配合使用

    原本在上一篇博客中要讲解一个Fragment的综合应用,但是中间又想到了滑屏方式,所以就分类在总结了一下,(http://smallwoniu.blog.51cto.com/3911954/13089 ...

  9. Android常见XML属性解析

    常见XML属性解析 属性 描述 android:id android:id的设置,通常有三种方式,详见下文 android:layout_width 控件宽度 android:layout_heigh ...

  10. 【安卓开发 】Android初级开发(零)各种布局

    线性布局的重要属性 (LinearLayout) 相关属性链接 layout_width 和 layout_height是布局器相对于外部构件的一个宽高距离. layout_margin是指与外部控件 ...

最新文章

  1. Gtk+2 Hello World:从源码到打包发布
  2. CC通信软件list
  3. python入门到实践-Python编程从入门到实践(基础入门)
  4. PHP学习方向-进阶2(三)
  5. Oracle电子商务套件版本12.1.3自述文件 (Doc ID 1534411.1)
  6. 《梦断代码》读后感 - 驱动,责任,交流,远虑
  7. 训练日志 2018.10.7
  8. 五年时间完成业务数字化转型,华为如今做得怎么样了?
  9. 95-240-052-原理-State-MemoryStateBackend
  10. 搭建Nodejs环境 创建Express应用
  11. C++ STL 文件内容的显示和追加
  12. 万以内的字符串整数变成汉子字符串
  13. 好程序员大数据教学点睛:Hadoop基础篇
  14. 雅虎邮箱2022年2月28日起停止中国大陆服务后的收信方法
  15. 1、AD创建模板和导入
  16. 物联网开发笔记(29)- 使用Micropython开发ESP32开发板之控制240x240的oled屏幕显示动态图片GIF(ST7789芯片)
  17. 时间与空间的相对性——思想实验推导狭义相对论(四)
  18. 解决VM虚拟机启动后假死
  19. 【Java 数据结构】树和二叉树
  20. 开题报告写作攻略--概述及国内外研究现状

热门文章

  1. 单片机实例31——6位数显频率计数器(硬件电路图+C语言程序)
  2. ### LinuxCBT VBOX Edition ###
  3. 在css中sticky定位的特性,CSS定位属性sticky详解
  4. 2020-11-16 MHF-NET乱读集锦
  5. Java里面的Lambda表达式
  6. JSON校验和JSON在线编辑器
  7. 修复计算机系统还原,怎么给电脑系统做一键恢复
  8. 新猿木子李:0基础学python培训教程 Python操作Excel之格式转换
  9. git_ Git 工具 - 储藏(Stashing)
  10. 如何卸载重装Adobe Acrobat