2 个 TextView,左侧带省略号,右侧带 nowrap,单行

2024-02-04

第一次在这个论坛发帖,希望一切顺利:)

我正在为我所在城市的公共交通开发 Android 应用程序。

这是我所拥有的 http://s28.postimg.org/i8cdifwgd/actual.png

[ |short destination   ||next departure| ]
[ |way too long dest...||next departure| ]

这就是我想要的: http://s13.postimg.org/z6vfcr3fb/expected.png

[ |short destination||next departure|    ]
[ |way too long dest...||next departure| ]

这是一个更完整的示例:s28.postimg.org/5gejnvfd9/actual2.png

奇怪的彩色背景只是为了轻松识别布局/文本视图。您也可以忽略棕色线(没关系)。

基本上,我想要具有可变长度的目的地[红色背景],在其右侧,我想要第一个出发时间[绿色背景]。一切都在一条线上。

我需要始终完整显示第一个出发信息(nowrap)。目的地可以用省略号 (...) 括起来。 [可选问题,如何将省略号“...”替换为“.” ?]

这是迄今为止我拥有的最好的工作代码:

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/txtTitleDestination"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@+id/txtTitleFirstDeparture"
            android:background="#FF0000"
            android:ellipsize="end"
            android:maxLines="1"
            android:padding="0dp"
            android:scrollHorizontally="true"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/txtTitleFirstDeparture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="0dp"
            android:layout_alignParentRight="true"
            android:background="#00FF00"
            android:ellipsize="none"
            android:maxLines="1"
            android:padding="0dp"
            android:textSize="18sp"
            android:textStyle="bold" 
            />

    </RelativeLayout>

我尝试过 TableLayout 和 LinearLayour 而不是relativelayout,但没有成功:(

知道我该怎么做吗?

提前致谢!

Louloox

[解决了] 只需要稍微修改一下瓦尔贝托斯 answer:

titleDestination.post(new Runnable() {
        @Override
        public void run() {             
            int widthTextViewDeparture = measureTextWidthTextView(titleFirstTime, pContext);
            int widthTextViewDestination = titleDestination.getWidth();
            int widthTextViewParent = rl_parent.getWidth();
            if(widthTextViewDestination + widthTextViewDeparture > widthTextViewParent) {
                titleDestination.setWidth(widthTextViewParent - widthTextViewDeparture);
                titleDestination.setEllipsize(TruncateAt.END);
                titleDestination.setHorizontallyScrolling(true);
            }
        }
    });

仅在必要时设置省略号可以正确截断文本。

Before: 
Lingolsheim Thiergaten --> Lingolsheim...     [1'23"] 21h23

With the modification:
Lingolsheim Thiergaten --> Lingolsheim Thi... [1'23"] 21h23

再次感谢 :)


您不需要以编程方式执行此操作,ConstraintLayout是一个魔法!

只需使用此代码

app:layout_constraintHorizontal_bias="0"左对齐视图

app:layout_constrainedWidth="true"使左侧文本换行

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <TextView
        android:id="@+id/leftText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        app:layout_constrainedWidth="true"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintEnd_toStartOf="@id/rightText"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="|short destination|" />

    <TextView
        android:id="@+id/rightText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/leftText"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="|next departure|" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是结果

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

2 个 TextView,左侧带省略号,右侧带 nowrap,单行 的相关文章

随机推荐

  • 在 XmlDocument 中加载 xml 文件时出错

    您好 我有下面的 xml 文件 我正在尝试使用下面的代码将其加载到 xml 文档中 XmlDocument Doc new XmlDocument Doc LoadXml C MappingFiles InputFile xml 但它抛出错
  • 如何从我的应用程序打开网页?

    我想让我的 WPF 应用程序打开默认浏览器并转到某个网页 我怎么做 对于 NET 桌面版本 System Diagnostics Process Start http www webpage com 对于 NET Core 默认为Proce
  • C++ 中的自定义事件?

    是否可以在 C 中创建自定义事件 例如 假设我有变量 X 和变量 Y 每当 X 发生变化时 我想执行一个将 Y 设置为 3X 的函数 有没有办法创建这样的触发器 事件 触发器在某些数据库中很常见 这基本上是观察者模式的一个实例 正如其他人提
  • Pandas groupby 和多索引

    pandas 是否有机会通过 MultiIndex 对数据进行分组 我的意思是不仅传递给 groupby 函数键 还传递键和值来预定义数据帧列 a np array foo foo foo bar bar foo foo dtype obj
  • 未找到数据源名称且未指定默认驱动程序

    我被要求将使用 MVP 模式的 WinForms 应用程序移植到网页上 除其他外 该应用程序将 CSV 文件上传到数据表 然后执行一些工作 CSV文件上传到服务器OK然后用下面的代码读取 string connectionString Dr
  • 在函数作用域末尾执行代码的简单方法[重复]

    这个问题在这里已经有答案了 在测试代 码中 我偶尔想设置 模拟一些全局变量 在测试 范围结束时我想恢复这些变量 例如 BOOST AUTO TEST CASE HttpClientCsrf std string csrfSave Http
  • 反序列化 PHP 会话数据

    我正在使用 CodeIgniter 3 x 和数据库会话驱动程序 我想访问data列那BLOB类型 这是我的 blob 数据 ci last regenerate i 1435420891 identity s 13 email prote
  • 匿名共享内存?

    是否有一种 POSIX y 方法来分配不与特定文件名绑定的共享内存 IE 仅通过 UNIX 域套接字传递 SCM RIGHTS 消息才能在进程之间共享内存 None
  • Asp.net 单元测试中缺少对 System.Web.Mvc 的引用?

    我最近为我的 Asp NET MVC 应用程序做了一些单元测试 或者我尝试这样做 它一直告诉我 类型 System Web Mvc Controller 是在未引用的程序集中定义的 您必须添加引用程序集 System Web Mvc 版本
  • 在 AJAX 请求期间显示微调框?

    展示旋转器的最佳方式是什么 我准备了一个 div id spinner 它在加载过程中应该是可见的 你使用 jQuery 吗 如果是这样你可以使用 ajaxStart 和 ajaxStop http docs jquery com Ajax
  • 迭代 String.prototype

    我知道for in循环可以帮助迭代对象 原型和集合的属性 事实是 我需要迭代String prototype 虽然console log String prototype 当我这样做时显示完整的原型 for var prop in Stri
  • 如何在 ASP.Net 应用程序中使用 HTTPS

    我想在 ASP NET Web 应用程序中使用 HTTPS 但仅限于 Login aspx 页面 如何才能做到这一点 首先获取或创建证书 获取 SecureWebPageModule 模块http www codeproject com A
  • Fluent nhibernate:如何映射具有类型为接口的属性的实体?

    我有一个像这样的实体 public class Employee public int ID get set public IAccountManager AccountManager get set 我还为 DefaultAccountM
  • WP8.1 HttpClient Stream 仅获取 65536 字节数据

    我正在尝试在 Windows 运行时为 win8 1 和 wp8 1 的 MediaElement 编写一个实时 flv 流解复用器 我已经完成了解复用代码 flv 文件可以正确解复用为 h264 和 aac 标签数据 当我尝试播放网络文件
  • Xcode 斯威夫特。如何在基于视图的 NSTableView 中以编程方式选择单元格

    我可以单击一个单元格并编辑其内容 但是 可以通过编程方式选择单元格 而不是单击 选择单元格 基本上为单元格焦点提供准备编辑的机会 StackOverflow 上有人问了关于 UITableView 的同样问题 给出的答案是 let inde
  • 如何从 Python 执行程序? os.system 由于路径中的空格而失败[重复]

    这个问题在这里已经有答案了 我有一个Python脚本需要执行外部程序 但由于某种原因失败了 如果我有以下脚本 import os os system C Temp a b c Notepad exe raw input 然后它失败并出现以下
  • 如何使用 GeoTools 创建具有纬度、经度和半径的圆?

    现在我有 Polygon circle geometryBuilder circle myLong myLat radiusInMeters 10 它创建 纬度 28 456306 长 16 292034 半径 500 一个具有巨大纬度和经
  • JavaScript 中用于格式化数字的正则表达式

    我需要使用 JavaScript 在网页上显示格式化的数字 我想对其进行格式化 以便在正确的位置有逗号 我该如何使用正则表达式来做到这一点 我已经得到这样的东西 myString myString replace d 3 g 1 然后意识到
  • 将 select 语句包含在事务中有何意义?

    将 select 语句包含在事务中有何意义 我认为 select 语句只是从数据库中 获取 数据 它们没有机会回滚某些内容 因为您无法更改数据 那么 这是否意味着我们永远不需要在事务中放置 select 语句 我对吗 Thanks 你是对的
  • 2 个 TextView,左侧带省略号,右侧带 nowrap,单行

    第一次在这个论坛发帖 希望一切顺利 我正在为我所在城市的公共交通开发 Android 应用程序 这是我所拥有的 http s28 postimg org i8cdifwgd actual png short destination next