Android 两个水平文本视图,第一个文本视图使用XML动态宽度

Android 两个水平文本视图,第一个文本视图使用XML动态宽度,android,android-layout,Android,Android Layout,我想建立布局,其中是2个文本视图 第一个文本视图具有未定义的长度(?)。因此,它必须是单线和椭圆结束 第二个文本视图定义了宽度(包裹内容) 我需要将第二个textview附加到第一个textview的末尾,但若第一个textview的内容长度太长,则应将其省略 另外,第二个textview必须位于父容器的末尾 我知道这有点让人困惑。请看下面的图片 它可以不使用java/kotlin编码,只使用简单的xml吗? <TableLayout android:layout_wi

我想建立布局,其中是2个文本视图

第一个文本视图具有未定义的长度(?)。因此,它必须是单线和椭圆结束

第二个文本视图定义了宽度(包裹内容)

我需要将第二个textview附加到第一个textview的末尾,但若第一个textview的内容长度太长,则应将其省略

另外,第二个textview必须位于父容器的末尾

我知道这有点让人困惑。请看下面的图片

它可以不使用java/kotlin编码,只使用简单的xml吗?


<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="0">

        <TableRow>
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:maxLines="1"
                android:paddingStart="10dp"
                tools:text="abcdeabcdeabcdeabcdeabcdeabcdeabcde" />

            <TextView
                android:id="@+id/tvCount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingStart="10dp"
                android:paddingEnd="10dp"
                android:maxLines="1"
                tools:text="(1001)" />
        </TableRow>

    </TableLayout>

这里是约束布局实现:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/textView8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text="Abc"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:layout_width="0dp"
    android:layout_height="34dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toStartOf="@+id/textView8"
    app:layout_constraintHorizontal_bias="0.031"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:text="llksdnflskdnflsdnfkjsllksdnflskdnflsdnfkjskjdfnkl 
    jsnkljndkljd" />


这是一个带有
线性布局的解决方案

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:layout_weight="1"
        tools:text="Long" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="Short" />

</LinearLayout>


感谢您的编辑!您可以使用constraintlayout@RahulKumar您能举个例子吗?我曾尝试使用ConstraintLayout实现它,但没有成功:(如果我可以使用ConstraintLayout实现,那就太好了为什么不使用
方向:水平
:)的
线性布局
)@makvasic,因为它有错误的行为。使用LinearLayout,“短”文本视图将始终位于右侧。我需要将“短”文本视图对齐到“长”文本视图的末尾,但如果“长”文本视图太长,则必须将其省略,“短”文本视图必须对齐到右侧(您是我的救世主!)TableLayout真的很老了,老实说,现在我们有了RelativeLayout或者更好的——ConstraintLayout(它是最近更新的)。@ZUNJAE我同意,但你能给我举一个使用ConstraintLayout的工作示例吗?应该是这样的solution@ZUNJAE是的,您就在这里,这是使用
ConstraintLayout
@NileshRathod的解决方案哦,非常感谢!app:layout\u constraintWidth\u default=“wrap”这是我错过的。这是最好的解决方案!与问题不匹配:(我现在理解了
LinearLayout
的问题…今天学到了一些新东西。谢谢。抱歉,此方法不起作用。它就像LinearLayout的“long”textview weight=1。请查看我的根帖子上的解释注释