在 ListView 上对齐两个 TextView,一左一右,而不拉伸背景

2024-01-02

所以我有两个TextViews每行ListView。一个应该左对齐,另一个右对齐。两个都TextViews有一个圆角矩形作为背景,应该将文本包裹在里面。所以我想出了这个:

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

    <TextView
        android:id="@+id/text_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/bubble_purple"
        android:gravity="center" >
    </TextView>

    <TextView
        android:id="@+id/text_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/text_right"
        android:background="@drawable/bubble_blue"
        android:gravity="center" >
    </TextView>
</RelativeLayout>

它看起来适合长文本,但不适用于较短的消息:

我也尝试过像这样的 LinearLayout :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/text_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:background="@drawable/bubble_blue"
        android:gravity="center" >
    </TextView>

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/text_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:background="@drawable/bubble_purple"
        android:gravity="center">
    </TextView>
</LinearLayout>

这适用于短消息,但不适用于较长的消息:

是否可以以某种方式测量组合宽度TextViews并以编程方式在这些布局之间切换,或者我在这里做错了什么?


这能如你所愿吗?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
    android:id="@+id/text_right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:background="@drawable/bubble_blue"
    android:text="2"
    android:gravity="center" >
</TextView>

<TextView
    android:id="@+id/text_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:background="@drawable/bubble_purple"
    android:text="9"
    android:gravity="center" >
</TextView>
</LinearLayout>

通过添加layout_weight参数并使它们相等,您可以告诉布局引擎在两个视图之间平均分配任何空闲空间。如果您希望左侧块比右侧块大,则可以为其赋予更大的权重。

Edit:也许这样更好:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<LinearLayout 
    android:id="@+id/text_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.5" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#456"
        android:text="5 + 1"
        android:gravity="center" >
    </TextView> 

</LinearLayout>

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text=" = " >
</TextView>

<LinearLayout 
    android:id="@+id/text_right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layoutDirection="rtl"
    android:layout_weight="0.5">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#856"
            android:text="6"
            android:gravity="center" >
        </TextView>

</LinearLayout>

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

在 ListView 上对齐两个 TextView,一左一右,而不拉伸背景 的相关文章

  • 在应用程序简历中隐藏软键盘

    我有一个 Android 应用程序 使用 Xamarin 用 C 编写 我已将应用程序简化为包含 TextView 和用于横幅广告的 Google admod AdView 的 LinearLayout 我不希望软键盘出现在应用程序中 这不
  • 使用 Android 前台服务为 MediaPlayer 创建通知

    问题就在这里 我目前正在开发一个应用程序 该应用程序必须提供 A 广播播放器 来自 URL 的 AAC 直播 还有一个播客播放器 来自 URL 的 MP3 流 该应用程序必须能够在后台运行 Android 服务 并通过以下方式向用户公开持续
  • GCM 向主题发送消息:TOO_MANY_TOPICS 错误

    以前 GCM 每个应用程序有 100 万个主题订阅的限制 我发现他们现在已经取消了这一限制 基于发布 订阅模型 主题消息支持 每个应用程序无限订阅 https developers google com cloud messaging to
  • Android - 如何一次只允许播放一个 MediaPlayer 实例?

    我正在尝试创建一个简单的 Sound board Android 应用程序 使用 ListView 项目作为按钮 顺便说一句 我是一个新手程序员 我的想法是 我按下一个按钮 就会播放一个特定的声音文件 如果我在播放声音时按下任何按钮 它应该
  • Android 应用程序在后台运行时保存数据

    目前我正在开发 xmmp 客户端 当应用程序位于前台时 该客户端工作得很好 但由于事实上 当应用程序处于后台时 我在 Application 类中保存了大量数据 复杂的 ArrayList 字符串和布尔值作为公共静态 每个字段都被垃圾收集
  • 与 Admob 广告单元 ID 混淆

    我跟着tutorial https developers google com admob android quick start在我的应用程序中创建广告横幅 到目前为止 这有效 我可以看到测试广告 但是 本教程指示我在两个不同的位置使用两
  • 如何将安卓手机从睡眠状态唤醒?

    如何以编程方式将 Android 手机从睡眠状态唤醒 挂起至内存 我不想获取任何唤醒锁 这意味着手机在禁用 CPU 的情况下进入 真正的 睡眠状态 我想我可以使用某种RTC 实时时钟 机制 有人有例子吗 Thanks 为了让Activity
  • Delphi XE7 Android 全屏(隐藏软键)

    如何在XE7中全屏显示 隐藏顶部 标题 和底部 软键 工具栏 在 XE6 中 我可以通过在应用程序部分写入来调整 AndroidManifest 以使我的应用程序全屏显示并且没有操作栏 android theme android style
  • 在 Google Analytics 中跟踪应用程序版本

    我正在使用谷歌分析模块 https marketplace appcelerator com apps 5081 2014113336 https marketplace appcelerator com apps 5081 2014113
  • 线程自动利用多个CPU核心?

    假设我的应用程序运行 2 个线程 例如渲染线程和游戏更新线程 如果它在具有多核 CPU 当今典型 的移动设备上运行 我是否可以期望线程在可能的情况下自动分配给不同的核心 我知道底层操作系统内核 Android linux内核 决定调度 我的
  • Android Studio 在编译时未检测到支持库

    由于 Android Studio 将成为 Android 开发的默认 IDE 因此我决定将现有项目迁移到 Android studio 中 项目结构似乎不同 我的项目中的文件夹层次结构如下 Complete Project gt idea
  • Android 原理图内容提供程序库配置?

    Jake Wharton 在最近的一次演讲中提到了这个库 它看起来是避免大量样板文件的好方法 所以我尝试了一下 但没有任何成功 https github com SimonVT schematic https github com Simo
  • 获取 AlarmManager 中活动的 PendingIntents 列表

    我有办法获取活动列表PendingIntent在设备中 我开始工作AlarmManager我想看看我的PendingIntents 已正确创建和删除 也很高兴看到其他什么PendingIntent在那里 只是为了看看某些应用程序是否正在做一
  • 在我的Android中,当其他应用程序想要录制音频时如何停止录音?

    在我的应用程序中 服务通过 AudioRecord 持续录制音频 当我的应用程序运行时 其他与音频记录相关的应用程序 例如 Google 搜索 无法工作 如何知道何时有其他应用想要录制音频 以便我可以停止录制以释放资源 答案是MediaRe
  • 从 Firebase 数据库填充微调器

    public class MainActivity extends AppCompatActivity DatabaseReference reference Spinner areaSpinner ArrayList
  • minHeight 有什么作用吗?

    在附图中 我希望按钮列与图像的高度相匹配 但我也希望按钮列有一个最小高度 它正确匹配图像的高度 但不遵守 minHeight 并且会使按钮向下滑动 我正在为按钮列设置这些属性
  • 材质设计图标颜色

    应该是哪种颜色 暗 材质图标 在官方文档上 https www google com design spec style icons html icons system icons https www google com design s
  • 保护 APK 中的字符串

    我正在使用 Xamarin 的 Mono for Android 开发一个 Android 应用程序 我目前正在努力使用 Google Play API 添加应用内购买功能 为此 我需要从我的应用程序内向 Google 发送公共许可证密钥
  • android Accessibility-service 突然停止触发事件

    我有一个 AccessibilityService 工作正常 但由于开发过程中的某些原因它停止工作 我似乎找不到这个原因 请看一下我的代码并告诉我为什么它不起作用 public class MyServicee extends Access
  • 在 Google 地图上绘制线条/路径

    我很长一段时间都在忙于寻找如何在 HelloMapView 中的地图上的两个 GPS 点之间画一条线 但没有运气 谁能告诉我该怎么做 假设我使用扩展 MapView 的 HelloMapView 我需要使用叠加层吗 如果是这样 我是否必须重

随机推荐

  • 如何解读Windows任务管理器?

    我运行的是 Windows 7 RC1 它使用与 Vista 相同的 WTM 当我查看流程时 有一些列我不确定有什么区别 内存 工作集 内存 私有工作集 内存 提交大小 谁能告诉我它们是什么 从以下article http cybernet
  • Minimagick 上传期间出现错误“composite -compose Over -gravity North”

    我正在尝试通过以下方式上传图像gem carrierwave gt 1 1 0 and gem mini magick gt 4 7 0 上传图像 我收到这个错误 我正在使用此代码上传图像 MiniMagick Tool Convert n
  • Firefox 与 contextmenu 事件同时触发 click 事件

    接下来的代码记录窗口对象上触发的事件 FIDDLE https jsfiddle net Makha92 kh8roneh 1 var logEvent function var count 1 timer 0 buffer functio
  • 当我在java中加载excel时出现错误

    我正在尝试用 java 读取一个简单的 xlsx private void readExcelData String excel throws Exception FileInputStream file new FileInputStre
  • PayPal REST API 交叉参考交易与付款

    我已成功使用 REST PHP API 完成了 PayPal 付款 但是 我想知道如何将 REST 交易与 PayPal Web 用户界面交叉引用 在 REST 方面 我有付款 ID getID 在交易被批准之前收到 看起来像 PAY 5B
  • Cocos 2d-x 中带有贝塞尔曲线的圆角矩形

    是否可以使用 DrawNode 对象绘制一个圆角矩形 我认为使用贝塞尔曲线是可能的 但我做了一些尝试 但我认为我无法处理它 查看 API 我只发现这两个函数 绘制四边形贝塞尔曲线 const Vec2 origin const Vec2 c
  • RTSP 身份验证:摘要问题

    我需要向流媒体服务器验证我的 RTSP 流 挑战如下 RTSP 1 0 401 Unauthorized WWW Authenticate Digest realm Streaming Server nonce 76bfe6986d3e76
  • 如何使用 Open CV 检测哈欠

    我正在开发一个 iOS 应用程序 需要检测用户何时打哈欠 我所做的是包括 Open CV 并使用 Haar Cascade 查找面孔 然后在面孔内查找嘴巴 也使用 HaarCascade 我遇到的麻烦是 我相信像做 face y mouth
  • UWP 共享功能在 Windows 10 Mobile 中不起作用

    我使用一个按钮创建了一个非常简单的 UWP 应用程序 点击它应该显示内置的共享弹出窗口分享一个PDF file 事实上 我让它适用于 Windows 10 桌面 但不适用于移动设备 弹出窗口不会出现在屏幕上 PDF 文件以字节数组形式出现
  • 保存某个范围内的所有信息并稍后恢复

    有没有办法将字体分配给范围 假设我有一个对象 myFont 我可以写 with Range A1 Font Bold myFont Bold Size myFont Size same with other properties end w
  • 如何通过滚动更改导航栏背景?

    我是网络开发新手 对于我的一个项目 我想在用户滚动时更改导航栏的背景 我希望它看起来像这样 https www nlogic co understanding vlan hopping attacks https www nlogic co
  • Java 问题中的暴力数独求解器算法

    除了求解方法之外 算法中的一切似乎都运行良好 当它使用可解数独板执行程序时 它会说无法解决 我已经尝试了解决方法中我能想到的一切 我尝试过调试 但在测试第一行后失败 有什么建议么 这是到目前为止的完整代码 public class Sudo
  • 我应该如何对具有许多子功能的功能进行单元测试?

    我希望更好地理解我应该测试具有许多子步骤或子功能的函数 假设我有以下功能 Modify the state of class somehow public void DoSomething DoSomethingA DoSomethingB
  • Visual Studio 的 .vsmdi 文件有多重要?

    这是场景 用户 A 已通过 Visual Studio 2010 创建了单元测试 测试项目和单元测试源代码已签入版本控制 用户B 从版本控制中获取测试项目和单元测试源代码 然后 用户 B 打开测试项目并收到一条消息 加载 blah blah
  • 如何正确使用同步链接哈希图

    尝试通过子类化链接哈希映射来制作 lru 映射 地图通过 collections synchronized 运行 映射的所有用法都被同步块包围 如果它们全部被删除 单元测试也会失败 人们可能会认为它们是不必要的 因为地图是通过 collec
  • “react-router”不包含名为“BrowserRouter”的导出

    我正在使用 React router 版本 5 5 1 并尝试在我的index js file src index js 14 8 21 react router does not contain an export named Brows
  • 如何从 Linux 与 SDL 2 对 Windows 进行交叉编译

    我尝试在 Arch Linux 64 位 上使用 SDL 2 和 mingw w64 g 编译器编译一个简单的 C 程序 为此 我从以下位置下载了 SDL2 devel 2 0 4 mingw tar gzhere https www li
  • WPF:如何设置垂直滑块的动态数字范围?

    我目前正在处理一个 WPF MVVM 项目 该项目有一个由多个视图使用的用户控件 但具有不同的值范围 这是我需要的一个例子 正如您所看到的 控件必须根据我需要在滑块中显示的值以不同的行为做出响应 无论数字如何 这只是一个示例 问题之一是该项
  • 为什么在 C++ 中对模板施加类型约束是不好的?

    In 这个问题 https stackoverflow com questions 874298 c templates that accept only certain typesOP询问限制模板将接受哪些类 总结一下 Java 中的同等
  • 在 ListView 上对齐两个 TextView,一左一右,而不拉伸背景

    所以我有两个TextViews每行ListView 一个应该左对齐 另一个右对齐 两个都TextViews有一个圆角矩形作为背景 应该将文本包裹在里面 所以我想出了这个