根据父高度调整 Imageview 的大小

2024-02-08

我有一个如下定义的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:orientation="vertical" >


        <ImageView
            android:id="@+id/movieImageView"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_centerVertical="true"
            android:paddingBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:src="@drawable/blank_96_1382x" /> 

        <TextView
            android:id="@+id/movieTypeView"
            android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/movieImageView"
            android:layout_alignLeft="@+id/movieImageView"
            android:maxLines="1"
            android:text="Tv Series"
            android:textColor="@color/gray"
            android:textSize="12sp" 
            android:gravity="center"
            android:paddingTop="3dp"
            android:paddingBottom="3dp"
            android:visibility="gone"
            android:textStyle="bold" />

   <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"    
            android:orientation="vertical"
            android:layout_centerVertical="true"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@id/movieImageView"
            android:layout_marginLeft="5dp">

                <TextView
                    android:id="@+id/movieTitleView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"                        
                    android:maxLines="3"
                    android:text="Shutter Island starring leonardo di caprio"
                    android:textColor="@color/white"
                    android:textSize="17sp"
                    android:textStyle="bold"
                    android:typeface="sans" />


        ...
</RelativeLayout>

It would look like this enter image description here

我希望图像视图根据图像视图左侧的内容缩小。例如,如果缺少几个字段并且字段的高度总和小于图像高度,那么它应该缩小(如下所示)。

If sum of heights of the fields on left side of image is greater than specified image height then it should retain the specified height as below enter image description here

我尝试扩展图像视图并更改图像视图的高度/宽度,重新调整图像的纵横比,如此处所述自定义图像视图 https://stackoverflow.com/questions/9914810/how-to-set-both-min-and-max-size-for-an-imageview-in-android。但问题是,父级高度返回MeasureSpec.getSize(heightMeasureSpec)不返回左侧字段占据的确切高度(因为只有在返回所有子项的 onMeasure 后才可用)。所以这种方法并没有带我到任何地方。

我怎样才能实现这种期望的行为?


您应该将 imageView 与线性布局中的 textview 对齐。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true" >

    <LinearLayout
        android:id="@+id/lnrTextViews"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#cee"
            android:gravity="center"
            android:maxLines="1"
            android:paddingBottom="3dp"
            android:paddingTop="3dp"
            android:text="Tv Series"
            android:textColor="#fff"
            android:textSize="12sp"
            android:textStyle="bold"
            android:visibility="visible" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#f00"
            android:gravity="center"
            android:maxLines="1"
            android:paddingBottom="3dp"
            android:paddingTop="3dp"
            android:text="Tv Series"
            android:textColor="#fff"
            android:textSize="12sp"
            android:textStyle="bold"
            android:visibility="visible" />
    </LinearLayout>

    <ImageView
        android:id="@+id/movieImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/lnrTextViews"
        android:layout_alignTop="@id/lnrTextViews"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:paddingBottom="5dp"
        android:src="@drawable/songs" />

</RelativeLayout>

复制粘贴此代码并添加一些不同大小的图像和额外的文本视图。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

根据父高度调整 Imageview 的大小 的相关文章

随机推荐

  • 如何在 Golang 的单元测试中测试 net.Conn?

    我目前正在考虑为 Go 中的 net Conn 接口以及在该功能之上构建的其他函数创建一些单元测试 我想知道在 Google Go 中进行单元测试的最佳方法是什么 我的代码如下所示 conn net Dial tcp 127 0 0 1 8
  • 对 SQL Server 存储过程进行版本控制的最佳方法是什么? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 设置 rhc(红帽客户端工具)时出错

    我已经按照 Openshift 网站上的说明安装了 rhc 当我跑步时一切看起来都很好gem install rhc and hgem update rhc但当我尝试打电话时rhc我在下面收到以下消息 我尝试重新安装 ruby 和 git
  • SSD 驱动器会让 Maven 构建更快吗?

    好帖子在这里 https stackoverflow com questions 4557934 would a ssd vs fast hdd drive make my eclipse run compile faster据说使用 SS
  • 如何使用公共函数从 Bytes 返回 KB、MB 和 GB

    我正在编写一个返回文件大小 以 B KB MB GB 为单位 的 函数 VB Net 代码总是首先获取以字节为单位的大小 因此当文件的大小 以字节为单位 小于 100 时 它返回 B 如果它 gt 1000 则将其除以 1000 然后返回
  • EJB 3.1 |通过 JNDI 调用远程会话 bean 时出错

    我试图从 Java SE 简单类 调用一个简单的无状态会话 bean 这是我的豆子 import javax ejb Stateless author MMRUser Stateless public class CapitalBean i
  • Android 11 5G 获取小区参数

    我正在新的 Android studio 预览版上尝试网络类型 5G 上的 Android 11 我的目标是获取单元信息详细信息 但是 方法 getAllCellInfo 在模拟器上返回空 空列表 Android 11 之前的所有模拟器都会
  • Android 在可扩展列表视图中禁用自动滚动

    我在可扩展列表视图中使用 当我打开列表视图中的某个项目时 我的滚动会自动聚焦在打开的项目上 我可以阻止列表聚焦在新项目上并停留在同一个地方吗 我尝试从打开的视图中删除可聚焦的内容 但它不起作用 您需要重写 OnGroupClickListe
  • 无法从 EC2 实例元数据服务检索凭证

    我正在尝试使用 SDK 通过 AWS SES API 发送电子邮件 我的代码基于此处的官方文档 https docs aws amazon com ses latest DeveloperGuide examples send using
  • 最少使用的 unicode 分隔符

    我正在尝试在特定位置使用分隔符标记我的文本 稍后将使用该分隔符进行解析 我想使用最不常用的分隔符 我当前正在查看 2 或 U 0002 字符 使用起来足够安全吗 还有哪些其他建议 文本为 unicode 同时包含英语和非英语字符 A想要使用
  • 字体真棒图标在 Chrome 中显示为正方形?

    我正在尝试在按钮中使用字体很棒的图标 该图标在 Firefox 中工作正常 但当我在 Chrome 中使用它时 它显示为正方形 我环顾四周 唯一发现的是字体的路径可能不正确 但我后来尝试了 cdn 版本here http www boots
  • PacMan:主要使用哪些启发式方法?

    除了 A BFS DFS 等之外 Pacman 中还广泛使用其他哪些好的寻路算法 启发式算法 如果吃豆人可以找到不止一种水果 我认为我提到的那些不会起作用 我需要一些好的寻路算法 PacMan 可以使用它们以尽可能少的步数完成迷宫 我试图四
  • 接口上的属性有什么用?

    使用 Resharper 我提取了现有类的接口 这个类在一些成员上设置了一些属性 Resharper 也将这些属性放在接口成员上 我可以从界面中删除这些属性吗 实现接口时不会继承接口上的属性 对吧 它们不被实现类使用 但仍然可能很关键 例如
  • 反应路由器索引路由始终处于活动状态

    我正在尝试使用 React 来设计我的导航activeClassName属性 到目前为止 它按预期工作 但有一个问题 我的第一个导航链接指向根路线 因此 即使在另一个 URL 上 它也会将该 URL 例如 skills 和 root 注册为
  • JQUERY UI - 根据验证选择选项卡

    我将 Jquery UI 选项卡式表单与需要验证的每个选项卡上的输入放在一起 理想情况下 我想验证当前可见的选项卡 然后选择下一个不验证的选项卡 最好的方法是什么 我的代码如下 div ul li a href tab1 One a li
  • 发送带有源端口但不绑定的 UDP 数据包

    我想在 Python 中发送 UDP 数据包并指定源端口但不绑定 与 hping3 等效 hping3 s sourceport p remoteport udp file message bin d 1024 c 1 remoteaddr
  • Googletest Eclipse C++:如何同时具有测试和生产可执行文件?

    我有一个基本问题谷歌测试 https code google com p googletest 在 Eclipse 中 我正在使用测试运行者 https github com xgsa cdt tests runner wiki Tutor
  • 在控制台中访问新的 ember 路由器

    是否有等效的函数可以使用 ember 中的新路由器在控制台中获取 currentPath 之前 我能够做到这一点 App router get currentPath 但是 似乎使用新路由器 您无法以相同的方式访问路由器 事实上 App r
  • 使用 OpenSSL 将现有 TCP 套接字转换为 TLS

    我需要一些有关 OpenSSL TLS 套接字的信息 首先 我想通过 TCP 上的 TLS 与服务器建立连接 通常首先创建一个 TCP 套接字 然后使用 TCP 套接字连接到服务器 然后将 TCP 套接字转换为 TLS 在通过 TCP 套接
  • 根据父高度调整 Imageview 的大小

    我有一个如下定义的布局