即使 textview 这么长,如何在 textview 的末尾制作视图?

How to make view at the end of textview even if the textview is so long?

我有这个布局,其中有文本视图和旁边的图标。 然而,文本是动态变化的,所以有时它会太长而将图标推出屏幕。 我试图给文本增加重量,但它使我不想要的图标出现在屏幕右侧,我只希望它紧跟在文本之后,即使文本转到下一行也是如此。

有我的代码:

  <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/Text"
                    android:layout_marginLeft="25dp"
                    android:text="llllll"
                    />

                <android.support.v7.widget.AppCompatImageButton
                    android:id="@+id/icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"
                    android:background="@android:color/transparent"
                    android:src="@drawable/ic_arrow_drop_down_black_24dp" />

            </LinearLayout>

有什么想法:(?

您可以在 HTML

中使用 <img> 标签

要知道怎么做,请看这个qution (is-it-possible-to-display-inline-images-from-html-in-an-android-textview)

将文本视图放在一个相对布局中,匹配父级作为宽度和高度包装内容。 将文本视图设置为相同的尺寸,即将父级作为宽度和高度作为包装内容。

在相同的相对布局中使用图像按钮并将 alignParentEnd 设置为 true。你会看到它总是添加文本视图的末尾。

如果您选择这样做,请设置一些 maxEms 和椭圆结束,以便文本不会与按钮重叠。你会自己测试得到这个值,通常取决于文本大小。

既然你想把它作为一个按钮,我就建议这样做。如果你希望它只是一个没有用的图标,你应该查看文本视图的 drawableEnd 属性。

您可以使用 ConstraintLayout 来处理这个问题。

<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="wrap_content">

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/image"
        app:layout_constraintWidth_default="wrap"/>

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/text"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

只需在文本视图 "android:maxWidth" 中添加一个 属性 行,如下所示:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/Text"
                android:layout_marginLeft="25dp"
                android:text="llllll"
                android:maxWidth="100dp" //it can be your specific size
                />

            <android.support.v7.widget.AppCompatImageButton
                android:id="@+id/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_arrow_drop_down_black_24dp" />

        </LinearLayout>