如何使用 ConstraintLayout 使视图“wrap_content 但不大于”?

2024-04-01

我连续有 3 个视图:标题、版本和图像视图(用作按钮):

  1. 标题应该是wrap_content但遵守以下规则
  2. 版本应该是wrap_content,位于标题右侧和图像视图左侧
  3. imageview 具有固定大小,位于父级的右上角

问题是,如果标题太大,则版本会移至右侧,并且不遵守“版本位于 imageview 左侧”的规则:

所以我需要限制标题宽度并使版本可见而不是移动到右侧。

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:background="#b3b2b2">

    <!--  -->
    <TextView
        android:id="@+id/LibraryWithVersionItem.title"
        android:layout_width="0dp"
        android:textStyle="bold"
        android:textSize="@dimen/fontSize18"
        android:textColor="@color/mySecondaryDarkColor"
        android:layout_height="wrap_content"
        android:ellipsize="middle"
        tools:text="ExampleLibrary 01234567890123456789012345"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        />

    <Spinner
        android:id="@+id/LibraryWithVersionItem.versions"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:textSize="@dimen/fontSize16"
        android:textColor="@color/mySecondaryDarkColor"
        tools:listitem="@layout/library_version"
        android:layout_marginTop="@dimen/margin8"
        android:layout_marginLeft="@dimen/margin8"
        android:layout_marginRight="@dimen/margin8"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/LibraryWithVersionItem.title"
        app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.info"
        app:layout_constraintHorizontal_bias="0.0"/>

    <TextView
        android:id="@+id/LibraryWithVersionItem.sentence"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/LibraryWithVersionItem.title"
        tools:text="Some library description in one sentence\nbut two lines"
        android:layout_marginTop="@dimen/margin8"
        android:layout_marginLeft="@dimen/margin8"
        app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.install"
        android:layout_marginRight="8dp"
        app:layout_constraintHorizontal_bias="0.0"/>

    <TextView
        android:id="@+id/LibraryWithVersionItem.isInstalled"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/LibraryManager.installed"
        android:textColor="#1a7c1a"
        android:layout_marginTop="@dimen/margin8"
        android:layout_marginBottom="@dimen/margin8"
        android:layout_marginLeft="@dimen/margin8"
        android:layout_marginRight="@dimen/margin8"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/LibraryWithVersionItem.sentence"
        app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.install"
        app:layout_constraintHorizontal_bias="0.0"/>

    <!-- information button -->
    <ImageView
        android:id="@+id/LibraryWithVersionItem.info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/margin8"
        android:paddingLeft="@dimen/margin8"
        android:paddingRight="@dimen/margin8"
        android:paddingBottom="@dimen/margin8"
        android:scaleType="center"
        android:src="@drawable/ic_info_outline_white_24dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <!-- install button -->
    <ImageView
        android:id="@+id/LibraryWithVersionItem.install"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/margin8"
        android:paddingRight="@dimen/margin8"
        android:paddingTop="@dimen/margin8"
        android:paddingBottom="@dimen/margin8"
        android:scaleType="center"
        android:src="@drawable/ic_get_app_white_24dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/LibraryWithVersionItem.info"/>


</android.support.constraint.ConstraintLayout>

PS 1. layout_width="0dp" + app:layout_constraintWidth_default="wrap"似乎正是我所需要的(“wrap_content 但不打破约束”),但它不起作用(仍然大于所需的大小):

<TextView
        android:id="@+id/LibraryWithVersionItem.title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:ellipsize="middle"
        android:textColor="@color/mySecondaryDarkColor"
        android:textSize="@dimen/fontSize18"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_default="wrap"
        tools:text="ExampleLibrary 01234567890123456789012345"

PS 2.设置版本的最小约束宽度(app:layout_constraintWidth_min="60dp")也没有帮助 - 它是不可见的,因为它移得太右了。


标题和版本应该在链中并且app:layout_constraintWidth_default="wrap" used:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:background="#b3b2b2">

    <!-- information button -->
    <ImageView
        android:id="@+id/LibraryWithVersionItem.info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/margin8"
        android:paddingLeft="@dimen/margin8"
        android:paddingRight="@dimen/margin8"
        android:paddingBottom="@dimen/margin8"
        android:scaleType="center"
        android:src="@drawable/ic_info_outline_white_24dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <!--  -->
    <TextView
        android:id="@+id/LibraryWithVersionItem.title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ellipsize="middle"
        android:textColor="@color/mySecondaryDarkColor"
        android:textSize="@dimen/fontSize18"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_default="wrap"
        tools:text="ExampleLibrary 01234567890123456789012345"
        app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.versions"
        android:layout_marginRight="8dp"
        android:layout_marginLeft="8dp"
        android:paddingBottom="1dp"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintHorizontal_bias="0.0"/>

    <Spinner
        android:id="@+id/LibraryWithVersionItem.versions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/fontSize16"
        android:textColor="@color/mySecondaryDarkColor"
        tools:listitem="@layout/library_version"
        app:layout_constraintRight_toLeftOf="@id/LibraryWithVersionItem.info"
        app:layout_constraintLeft_toRightOf="@+id/LibraryWithVersionItem.title"
        android:layout_marginRight="0dp"
        app:layout_constraintBottom_toBottomOf="@+id/LibraryWithVersionItem.title"
        android:layout_marginBottom="0dp"/>

    <TextView
        android:id="@+id/LibraryWithVersionItem.sentence"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/LibraryWithVersionItem.title"
        tools:text="Some library description in one sentence\nbut two lines"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="@dimen/margin8"
        app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.install"
        android:layout_marginRight="8dp"
        app:layout_constraintHorizontal_bias="0.0"
        android:layout_marginStart="@dimen/margin8"
        android:layout_marginEnd="8dp"/>

    <TextView
        android:id="@+id/LibraryWithVersionItem.isInstalled"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/LibraryManager.installed"
        android:textColor="#1a7c1a"
        android:layout_marginTop="@dimen/margin8"
        android:layout_marginLeft="@dimen/margin8"
        android:layout_marginRight="@dimen/margin8"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/LibraryWithVersionItem.sentence"
        app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.install"
        app:layout_constraintHorizontal_bias="0.0"
        android:layout_marginStart="@dimen/margin8"
        android:layout_marginEnd="@dimen/margin8"/>

    <!-- install button -->
    <ImageView
        android:id="@+id/LibraryWithVersionItem.install"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/margin8"
        android:paddingRight="@dimen/margin8"
        android:paddingTop="@dimen/margin8"
        android:paddingBottom="@dimen/margin8"
        android:scaleType="center"
        android:src="@drawable/ic_get_app_white_24dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/LibraryWithVersionItem.info"/>

</android.support.constraint.ConstraintLayout>

我尝试将版本与标题基线对齐,但如果标题有 2 行或更多行,它会与第一行对齐,这是不需要的。所以我必须将版本与标题底部对齐并硬编码标题-3底部填充。

然而,它在 Android Studio 中看起来符合预期:

but on hardware device it looks different:enter image description here

When analyzing in Layout Inspector i can see title width is calculated wrong:enter image description here

可能是使用它的副作用RecyclerView但不管怎么说...

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

如何使用 ConstraintLayout 使视图“wrap_content 但不大于”? 的相关文章

  • android edittext中的字符映射

    我想让我的编辑文本就像我写字符 g 时一样 它是相关的映射自定义字符应该写成印地语中的 我认为应该有字符映射 但没有知识任何人都可以帮助我 怎么做 其他应用程序https play google com store apps details
  • Xamarin Android Webview Javascript

    我正在尝试通过 Xamarin for Android 创建一个移动应用程序 它有一个显示网站的 WebView 问题是正常按钮会触发 但 javascript 事件不会触发 我已经启用了 Javascript 但没有运气 如何在 Andr
  • 如何在android中显示保存在sdcard文件夹中的图像[关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions 当我正在显
  • fetchUuidsWithSdp 的奇怪 UUID 逆转

    我有一个在树莓派上运行的 python 蓝牙服务器 使用 PyBluez 我在服务器中使用的uuid是 8f86d132 4ab8 4c15 b8df 0b70cf10ea56 我正在打电话device fetchUuidsWithSdp
  • 如何在 Linux 内核中定义并触发我自己的新软中断?

    我想在 Linux 内核中创建自己的软中断 这是正确的方法吗 In the init我想触发该模块的softirq我将添加一个调用 394 void open softirq int nr void action struct softir
  • Recyclerview 动态部分不使用任何第三个库

    我想将标头添加到 recyclerview 我正在尝试使用来实现它 Override public int getItemViewType int position depends on your problem if position 0
  • Android应用程序组件销毁和重新创建的详细信息

    有人可以向我提供一些具体的 值得信赖的 最好是简洁的 信息 内容如下 系统销毁和 如果适用 重新创建组件的顺序 片段 活动 活动的线程 异步任务 计时器 静态数据 类何时卸载 其他类中的线程 异步任务 定时器 主机 TabActivity
  • Android 深度链接至 Instagram 应用

    Instagram 已经发布了 iOS 深层链接的 url 方案 但尚未为 Android 创建文档 有没有办法深入链接到 Android 上的 Instagram 应用程序 以转到 Instagram 应用程序中的特定位置 例如 Inst
  • 为什么是 javascript:history.go(-1);无法在移动设备上工作?

    首先 一些背景 我有一个向用户呈现搜索页面 html 表单 的应用程序 填写标准并单击 搜索 按钮后 结果将显示在标准部分下方 在结果列表中 您可以通过单击将您带到新页面的链接来查看单个结果的详细信息 在详细信息页面中 我添加了一个 返回结
  • 从 BroadcastReceiver 类调用活动方法

    我知道我可以做一个内部接收器类来调用接收器中的任何方法 但我的主要活动太大了 要做的事情也很多 因此 我需要一个扩展广播接收器的类 但它不是内部类 并且可以从我的主要活动中调用一种方法 我不知道是否可能 但我的活动是家庭活动和 single
  • 使用 AsyncTask 传递值

    我一直在努力解决这个问题 但我已经到了不知道该怎么办的地步 我想做的是使用一个类下载文件并将其解析为字符串 然后将该字符串发送到另一个类来解析 JSON 内容 所有部件都可以单独工作 并且我已经单独测试了所有部件 我只是不知道如何将值发送到
  • Android 中的处理程序与异步调用

    目前我正在使用处理程序来调用 Web 服务方法以使其在后台运行 问题是它需要更多的时间来给出响应 在性能方面似乎更昂贵 现在我计划使用异步调用 哪一个是最好的 Android 中的处理程序和异步调用有什么区别 请帮我想出一个最好的解决方案
  • 如何检查 Android 中的同步设置

    我正在构建一个 Android 应用程序 我需要检查设备中注册的每个单独帐户的同步设置 我知道我可以通过 ContentResolver 类来做到这一点 但我遇到了一些问题 我已设法获取设备上所有帐户的列表 但我不知道在运行时从哪里获取特定
  • Android - 以编程方式选择菜单选项

    有没有办法以编程方式选择菜单选项 基本上 我希望视图中的按钮能够执行与按特定菜单选项相同的操作 我正在考虑尝试调用 onOptionsItemSelected MenuItem item 但我不知道要为菜单项添加什么 是的 有一种方法可以选
  • Android:如何从网络异步获取搜索建议?

    我创建了一个可搜索的活动 现在 我想添加从网络服务获取的搜索建议 我想异步获取这些建议 根据添加自定义建议 http developer android com guide topics search adding custom sugge
  • 如何使用 AccessibilityService 在 Android 中模拟按键

    我正在编写一个辅助服务 我一直在尝试在应用程序上进行一些自动搜索 我使用accessibilityservice action paste来填充EditText 然后我需要模拟软键盘上的按键 但我不知道如何做 你们能帮我一下吗 你可以尝试A
  • 错误:(23, 13) 无法解决:com.google.android.gms:play-services:11.2.0“安装存储库和同步项目”不起作用

    我正在尝试在我的 Android 应用程序中获取位置并更新到服务器 这是我的 Gradle 代码 我在这里包含了compile com google android gms play services 11 2 0 这条线是从文档中 htt
  • Dagger 2 中“HasFragmentInjector”的实际用法是什么

    我之前已经实现了 dagger2 v2 2 但现在他们也添加了 dagger android 部分 所以我正在用它创建示例项目 我知道旧的方法论 Provide and Modules and 成分等注释 但从 Dagger 2 8 开始
  • Android 屏幕方向错误

    我使用的是 Android HTC HERO 2 1 版本 我写的活动
  • 有没有任何代码可以在android中设置壁纸而无需裁剪和缩放?

    我正在创建一个画廊应用程序 我的第一个应用程序 这是我的代码 Bitmap bmd BitmapFactory decodeStream is try getApplicationContext setWallpaper bmd catch

随机推荐

  • PHP 中的 zipArchive 打开错误

    无法在 PHP 中创建 zip 存档 总是返回Ziparchive ER MULTIDISK fileName SERVER DOCUMENT ROOT temp temp zip zip new ZipArchive err zip gt
  • Jquery - 遍历所有选中的单选按钮

    我有一个类似于下面的表格
  • 如何通过 Telegraf 监控目录的大小

    我们需要监控一个目录的大小 例如InfluxDB的数据目录 以便在Grafana中设置警报 正如这里提到的 如何配置 telegraf 将文件夹大小发送到 influxDB https stackoverflow com q 4427343
  • Dart:pub get 与 pub Upgrade

    根据pub get docs http pub dartlang org doc pub get html之间的主要区别pub get and pub upgrade is 如果锁定文件已存在 则 pub get 使用锁定在其中的依赖项版本
  • setMaxForRoute 在 ThreadSafeClientConnManager 中不起作用

    我一直在尝试使用 Apache HttpClient 4 1 3 实现连接池并且ThreadSafeClientConnManager 当我尝试设置路由的最大连接数时 我遇到了一个问题 基本上我遵循的例子hc apache org http
  • 缺少具体实现“getter Equatable”/道具问题

    我正在使用 flutter 完成许多关于 bloc 的教程 但遇到了一些不一致的情况 我正在使用 Android studio 并使用 Intellij v1 6 0 的插件创建块代码 对于 bloc event 我继续看到类似这样的示例
  • 使用日期时间搜索日志文件

    我正在读取日志文件 并希望选择将搜索限制在特定日期范围内 日志文件中的行采用以下格式May 27 09 33 33 我已经将日志文件每一行中的日期与其余文本分开 我只是希望能够写出这样的声明 if the date falls betwee
  • 转换为未显式实现的接口?

    假设您定义了一些任意接口 public interface IInterface void SomeMethod 假设有一些类恰好有一个匹配的公共接口 即使它们没有 实现IInterface IE public class SomeClas
  • 在 C# 中使用 ImageAttributes 旋转色调

    如何使用 GDI 旋转图像的色调ImageAttributes 并且大概ColorMatrix 请注意 我想旋转色调 而不是对图像进行着色 EDIT 通过旋转色调 我的意思是图像中的每种颜色应该转换为不同的颜色 而不是使整个图像变成一种颜色
  • 如何将抓取的项目放入 Pyqt5 小部件中?

    我正在尝试为 Scrapy 爬虫制作一个简单的 GUI 用户可以按 开始 按钮来运行抓取并在 textBrowser 或其他 qt 小部件 请告知 中查看抓取的结果 我的蜘蛛 import scrapy json class CarSpid
  • 如果元素的位置是绝对的,浏览器的渲染是否会回流?

    如果我有一个具有绝对位置的元素 并且更改其左侧和顶部位置 则会回流到其父子元素吗 如果它自己的孩子不受影响 因为它们也是由左轴和上轴绝对定位的 那么它们又怎么样呢 如果我更改元素的宽度 高度但在父元素及其子元素中不重要 具有绝对位置的对象不
  • 如何在 Typescript 中创建抽象工厂模式?

    我正在尝试在 Typescript 中实现标准抽象工厂模式 但编译器不合作 这是我的代码的简化版本 abstract class Model class User extends Model abstract class ModelFact
  • 如何取消设置全局变量。

    我有一个id一个项目和一个id客户端的会话是以 JSON 格式传递的 php 会话 这些存储在全局变量中id p and id c所以我可以使用这些 id 进行多次插入和更新选择等 当用户选择另一个项目或更改页面时 我需要取消设置这些变量
  • 如何使用正则表达式来忽略包含特定子字符串的字符串?

    我将如何使用负向后查找 或任何其他方法 正则表达式来忽略包含特定子字符串的字符串 我读过之前的两个 stackoverflow 问题 java 正则表达式用于文件过滤 https stackoverflow com questions 36
  • macports PHP5 的 Pear 安装

    我通过 macports 在 opt local macports 的默认位置安装了 PHP5 pear 没有端口文件 如果我从 pear 站点进行标准 pear 安装 则 pear 应该放置在什么目录位置才能与 PHP 一起使用 虽然cu
  • 我如何知道哪个初始化程序是指定的初始化程序?

    我如何知道哪个初始值设定项是任何类的指定初始值设定项 我猜它是需要最多参数的一个 但有时这可能是不正确的 omz的答案可以更坚定地表述 The Documentation for a Framework classwill指定哪个是指定的初
  • C++:从用户输入调用函数

    在Python中 当我有几个根据用户输入调用的不同函数时 我有一个字典 其中用户输入作为键 函数名称作为值 def x y return y def z y return y functions x x z z print function
  • 可以从 CUDD 管理器中删除变量吗?

    谁能告诉我是否可以安全地从 CUDD 中的管理器中删除变量 例如 我通过以下方式注册两个变量v1 Cudd bddNewVar manager and v2 Cudd bddNewVar manager 我可以删除吗v2来自经理 我认为不可
  • 如何使用分隔符连接 PySpark 中的多个列?

    我有一个pyspark Dataframe 我想加入3个专栏 id column 1 column 2 column 3 1 12 34 67 2 45 78 90 3 23 93 56
  • 如何使用 ConstraintLayout 使视图“wrap_content 但不大于”?

    我连续有 3 个视图 标题 版本和图像视图 用作按钮 标题应该是wrap content但遵守以下规则 版本应该是wrap content 位于标题右侧和图像视图左侧 imageview 具有固定大小 位于父级的右上角 问题是 如果标题太大