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

Android实现两个TextView并排:首文本多行显示且次文本不被遮挡

解决两个TextView并排布局:长文本自动换行且第二个始终可见的问题

针对你的布局需求,只需调整第一个TextView的布局属性即可实现预期效果,修改后的代码如下:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="start"
    android:orientation="horizontal"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:scrollHorizontally="false"
        android:layout_height="wrap_content"
        android:singleLine="false"
        android:text="long long long long long long myText1" />

    <TextView
        android:text="myText2"
        android:singleLine="true"
        android:textStyle="bold"
        android:layout_marginStart="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

关键修改说明:

  • 给第一个TextView设置android:layout_width="0dp"和android:layout_weight="1":让它占据LinearLayout中除第二个TextView之外的全部剩余水平空间,避免因文本过长挤压第二个控件。
  • 保留第一个TextView的android:singleLine="false":确保文本超出分配宽度时自动换行,而非横向拉伸或隐藏内容。
  • 第二个TextView保持wrap_content宽度:保证其始终完整显示,不会被压缩或挤出屏幕视野。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 04:25:03