You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android水平LinearLayout中两个TextView大文本布局重叠问题求助

解决LinearLayout中TextView被挤压的问题

直接修改第一个TextView的布局参数即可,核心是利用layout_weight让第一个TextView占据父布局的剩余空间,同时保留单行和文本省略的设置,确保第二个TextView始终可见:

<?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">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="1"
        android:ellipsize="end"
        tools:text="@tools:sample/lorem/random"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        tools:text="random text"/>
</LinearLayout>

原理说明

  • 将第一个TextView的layout_width设为0dp,配合layout_weight="1",LinearLayout会先计算第二个TextView的实际宽度,剩余的全部分配给第一个TextView。
  • 加上maxLines="1"和ellipsize="end",当第一个TextView文本过长时,会自动截断并显示省略号,不会挤压第二个TextView的空间。

内容的提问来源于stack exchange,提问作者Vladimir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 13:33:21