使用 ConstraintLayout 将 2 个视图并排放置

3
我正在尝试在ConstraintLayout中将2个TextView并排放置,左侧视图具有动态文本(可以是短文本或非常长的文本,如果重叠第二个视图,则应该使用省略号)。 我希望这两个TextView会并排放置,因此第二个视图将从第一个视图的结尾开始。请帮忙 :-)

1
你的意思是希望它们各自占据50%的空间吗?你还没有解释第二个TextView应该有多宽。 - Blundell
请包含您之前尝试的XML以及您想要最终布局看起来的描述(或图像)。 - shriakhilc
1
我认为这是你需要的:https://dev59.com/z67la4cB1Zd3GeqPe47U - Manohar
@ManoharReddy 这正是我正在寻找的!谢谢! - Sharas
谢谢大家,通过@ManoharReddy的帮助找到了答案。 https://dev59.com/z67la4cB1Zd3GeqPe47U - Sharas
3个回答

4
尽管这是最简单的方法,但如果您希望它们占用相同的空间,则可以使用layout_constraintVertical_chainStyle。
 <?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.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/text_view1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Random 1"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/text_view2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Random 1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/text_view1"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

请看这个链接:https://medium.com/@loutry/guide-to-constraintlayout-407cd87bc013。 ChainStyle通常用于您希望水平或垂直均匀分布元素时。链由在链的第一个元素上设置的属性控制(即链的“头”) - 对于水平链,它是最左边的小部件;对于垂直链,它是最上面的小部件。

1
尝试这样做...根据您的布局,只需调整视图的顶部和底部即可。
<TextView
    android:id="@+id/leftView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="5dp"
    android:text="Left View"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@id/rightView"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/rightView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="5dp"
    android:text="Right View"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@id/leftView"
    app:layout_constraintTop_toTopOf="parent" />

0
尝试像这样做:

    <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
       android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@id/txt1"
        app:layout_constraintRight_toRightOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接