首页 > 解决方案 > 如何使用大单行文本将 ButtonView 定位到 TextView 的末尾,而不是将其移出屏幕

问题描述

我在TextView的末尾有Button
小文字

当文本增加时,我得到以下信息:
屏幕后面的按钮

但是我希望我的按钮仅位于文本的末尾,并且在文本很大时不会移出屏幕:
需要的结果

这是我的布局:

<LinearLayout
    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"
    android:padding="10dp"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="Long text without button"
        android:textColor="#000"
        android:textSize="36sp" />

    <FrameLayout
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content" >

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:ellipsize="end"
            android:text="Button" />

    </FrameLayout>
</LinearLayout>

如果我将layout_weightFrameLayout移动到AppCompatTextView那么我会得到需要的结果,但是使用小文本我会得到以下信息:
结果不好

我尝试使用LinearLayout权重和ConstraintLayout,但没有任何帮助。
如何做到这一点?

标签: androidandroid-layoutandroid-constraintlayout

解决方案


你只需使用ConstraintLayout.

尝试这个

<?xml version="1.0" encoding="utf-8"?>
<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">

    <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/text"
            android:layout_width="0dp" <-- It means using match-constraint.
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="Long text without button"
            android:textColor="#000"
            android:textSize="36sp"
            app:layout_constraintEnd_toStartOf="@+id/button"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="Button"
            app:layout_constraintBottom_toBottomOf="@id/text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/text"
            app:layout_constraintTop_toTopOf="@id/text" />

</androidx.constraintlayout.widget.ConstraintLayout>

结果

在此处输入图像描述

附加 XML - 版本wrap_content

<?xml version="1.0" encoding="utf-8"?>
<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">

    <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="short text"
            android:textColor="#000"
            android:textSize="36sp"
            app:layout_constraintHorizontal_chainStyle="packed"
            app:layout_constrainedWidth="true"
            app:layout_constraintEnd_toStartOf="@+id/button"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="Button"
            app:layout_constraintBottom_toBottomOf="@id/text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/text"
            app:layout_constraintTop_toTopOf="@id/text" />

</androidx.constraintlayout.widget.ConstraintLayout>

修改layout_constraintHorizontal_bias的属性TextView以适合您想要的布局。


推荐阅读