解决ConstraintLayout两个组件挤压问题

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中文字靠右显示
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ConstraintLayout 是 Android 中一种强大的布局容器,可以用于实现复杂的布局效果。要实现两个控件水平居中对齐,可以按照以下步骤进行操作: 1. 在 XML 布局文件中,将两个控件添加到 ConstraintLayout 中,并为它们设置 id。 2. 使用约束条件将两个控件水平居中对齐。可以通过以下方式实现: - 为第一个控件设置左边界约束,将其与父容器的左边界对齐。 - 为第二个控件设置右边界约束,将其与父容器的右边界对齐。 - 为两个控件设置水平居中约束,将它们相互对齐。 下面是一个示例代码: ```xml <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="match_parent"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView 1" app:layout_constraintLeft_toLeftOf="parent" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView 2" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toRightOf="@id/textView1" app:layout_constraintHorizontal_bias="0.5" /> </androidx.constraintlayout.widget.ConstraintLayout> ``` 在上述示例中,textView1 的左边界与父容器的左边界对齐,textView2 的右边界与父容器的右边界对齐,并且 textView2 的左边界与 textView1 的右边界对齐。通过设置 `app:layout_constraintHorizontal_bias="0.5"`,可以使两个控件在水平方向上居中对齐。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值