解决ConstraintLayout两个组件挤压问题

本文介绍在使用ConstraintLayout进行UI布局时,如何通过设置特定属性来避免一个组件因另一个组件过长而被挤出屏幕的问题。具体包括将第一个TextView的宽度设为0dp,并设置正确的约束条件,以确保两个组件能在同一行正确显示。

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

ConstraintLayout一行配置两个组件的时候,如果都是wrap_content,那么第一个如果太长就会把第二个挤掉,解决方法如下:
<TextView
    android:id=“@+id/titleView”
    android:layout_width=“0dp”  必须指定宽度为0,使得第一个textview自适应
    android:layout_height=“wrap_content”
    android:layout_marginLeft=“@dimen/job_detail_left_padding”
    android:layout_marginStart=“@dimen/job_detail_left_padding”
    android:layout_marginTop=“16dp”
    android:ellipsize=“end” 
    android:gravity=“center_vertical”
    android:maxLines=“1”
    android:text=”我是很长很长很长很长很长很长很长很长很长的一段文字“
    android:textColor=”#1A1A1A“
    android:textSize=”18sp“
    android:paddingRight=”9dp“
    card_view:layout_constraintHorizontal_bias=”0.0“这个主要是使得该view中文字靠左显示
    card_view:layout_constraintLeft_toLeftOf=”parent“ 需要同时指定左侧和右侧链
    card_view:layout_constraintRight_toLeftOf=”@+id/updateTimeView“
    card_view:layout_constraintTop_toTopOf="parent"/>

<TextView
    android:id="@+id/updateTimeView"
    android:layout_width="wrap_content" 必须是wrap_content,否则会把第一个TextView挤压掉
    android:layout_height="wrap_content"
    android:text=”我在后边不想被挤掉"
    android:textColor="#B3B3B3"
    android:textSize="@dimen/text_size_12sp"
    card_view:layout_constraintRight_toRightOf="parent"
    android:layout_marginRight="@dimen/job_detail_right_padding"
    card_view:layout_constraintLeft_toLeftOf="@+id/titleView"需要指定左侧链
    android:layout_marginLeft="8dp"
    card_view:layout_constraintBaseline_toBaselineOf="@+id/titleView"
    android:layout_marginEnd="@dimen/job_detail_right_padding"
    card_view:layout_constraintHorizontal_bias="1.0" />这个主要是使得该view中文字靠右显示
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值