一行中,中间的TextView自适应功能实现(ConstraintLayout)

本文介绍了一种使用ConstraintLayout和LinearLayout实现特定布局需求的方法,确保左侧TextView文本溢出时显示省略号,右侧TextView则保持单行不换行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何实现

在这里插入图片描述
怎么实现这种需求呢?最笨的办法就是动态计算,很麻烦而且导致代码很不优雅。就是动态计算出TextView的宽度。
有没有从布局上就能实现上图的需求呢?
下面文章中方案还没试,以后碰到再试
2 TextViews, left with ellipsis, right with nowrap, single line

2022年9月2日更新
碰到一个和上面一模一样的需求,按照上面文章的实现方案可以实现

<?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"
    android:id="@+id/ll_user_action"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_comment_user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/c_4a90e2"
        android:textSize="13sp"
        android:ellipsize="end"
        android:maxLines="1"
        android:includeFontPadding="false"
        android:text="迪丽热"
        app:layout_constrainedWidth="true"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/tv_comment_user_action"/>

    <TextView
        android:id="@+id/tv_comment_user_action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/c_999999"
        android:textSize="13sp"
        android:layout_marginLeft="2dp"
        android:includeFontPadding="false"
        android:text="点赞了你的评论"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/tv_comment_user_name"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

在这里插入图片描述
在这里插入图片描述
一层完美实现

方案二需要两层实现

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tv_comment_user_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/c_4a90e2"
            android:textSize="13sp"
            android:layout_weight="1.0"
            android:ellipsize="end"
            android:maxLines="1"
            android:includeFontPadding="false"
            android:text="迪丽热巴迪丽热巴迪丽热巴迪丽热巴迪丽热巴迪丽热巴迪丽热巴迪丽热巴" />

        <TextView
            android:id="@+id/tv_comment_user_action"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/c_999999"
            android:textSize="13sp"
            android:layout_marginLeft="2dp"
            android:includeFontPadding="false"
            android:text="点赞了你的评论" />

    </LinearLayout>
</LinearLayout>

2021年3月3日更新
在开发中又遇到了类似的需求,看下图
在这里插入图片描述
左右两边宽度固定后,需要中的内容自适应。

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/guide_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dp_11"
        android:src="@drawable/icon_more_2"
        app:layout_constraintRight_toRightOf="@+id/guide_other_text_view"
        app:layout_constraintTop_toBottomOf="@+id/guide_other_text_view">

    </ImageView>

    <RelativeLayout
        android:id="@+id/guide_portrait_container"
        android:layout_width="102dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp_15"
        android:layout_marginTop="@dimen/dp_15"
        android:layout_marginBottom="@dimen/dp_30"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </RelativeLayout>

    <TextView
        android:id="@+id/guide_other_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/dp_15"
        android:textColor="@color/bg_1a1a1a"
        android:textSize="@dimen/sp_13"
        app:layout_constraintBottom_toBottomOf="@+id/guide_portrait_container"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guide_portrait_container">

    </TextView>

    <TextView
        android:id="@+id/guide_title_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp_7"
        android:ellipsize="end"
        android:includeFontPadding="false"
        android:singleLine="true"
        android:textColor="@color/bg_1a1a1a"
        android:textSize="@dimen/sp_13"
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toBottomOf="@+id/guide_portrait_container"
        app:layout_constraintLeft_toRightOf="@+id/guide_portrait_container"
        app:layout_constraintRight_toLeftOf="@+id/guide_other_text_view"
        app:layout_constraintTop_toTopOf="@+id/guide_portrait_container">

    </TextView>

    <TextView
        android:id="@+id/guide_goto_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/dp_3"
        android:text="去关注流看看"
        android:textSize="@dimen/sp_12"
        android:textColor="@color/c_4a90e2"
        app:layout_constraintBottom_toBottomOf="@+id/guide_arrow"
        app:layout_constraintRight_toLeftOf="@+id/guide_arrow"
        app:layout_constraintTop_toTopOf="@+id/guide_arrow">

    </TextView>

    <View
        android:layout_width="0dp"
        android:layout_height="1px"
        android:background="#FFE6E6E6"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

看guide_title_text_view设置了左右相对位置后,如果layout_width设置的是wrap_content,需要设置layout_constrainedWidth为true,否则guide_title_text_view过长的部分会盖到左右两边的view上;或者把layout_width设置成0dp。
0dp就是match parent,内容在左边;wrap content 内容是居中的。

参考

2 TextViews, left with ellipsis, right with nowrap, single line

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值