如何在文本视图的末尾进行视图,即使文本视图太长



我有这个布局,我有文本视图和旁边的图标。但是,文本是动态变化的,因此有时它会太长,从而将图标推出屏幕。我试图为文本添加权重,但它使图标出现在屏幕右侧,我不想要,我只想让它紧跟在文本之后,即使文本进入下一行。

有我的代码:

  <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>

知道:(吗?

您可以使用

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>

您可以在 HTML 中使用<img>标签

要知道如何做到这一点,请参阅此 qution(是否可以显示内联图像来自 html-in-an-android-textview(

将文本视图放在相对布局中,并将父项作为宽度和高度换行内容。将文本视图设置为与换行内容相同的尺寸,即行进父级作为宽度和高度。

使图像按钮处于相同的相对布局中,并使用 alignParentEnd 作为 true。您会看到它始终添加文本视图的末尾。

如果选择执行此操作,请设置一些 maxEms 和省略号结尾,以便文本不会与按钮重叠。您将通过自己测试来获得值,通常取决于文本大小。

由于您希望将其用作按钮,因此我建议这样做。如果你希望它只是一个没有用的图标,你应该看看文本视图的drawableEnd属性。

最新更新