Как расположить ButtonView в конце TextView с большим однострочным текстом и не перемещать его за пределы экрана - PullRequest
1 голос
/ 28 сентября 2019

У меня есть кнопка в конце TextView :
small text

Когда текст увеличивается, я получаюследующее:
button behind the screen

Но я хочу, чтобы моя кнопка находилась только в конце текста и не выходила за пределы экрана при большом размере:
needed result

Это мой макет:

<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_weight из FrameLayout в AppCompatTextView тогда я получаю необходимый результат, но с небольшим текстом я получаю следующее:
bad result

Я пытался с LinearLayout весамии ConstraintLayout , но мне ничто не поможет.
Как этого добиться?

Ответы [ 2 ]

1 голос
/ 28 сентября 2019

Вы просто используете 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>

Результат

enter image description here

Дополнительно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 и layout_constraintHorizontal_bias TextView, чтобы они соответствовали нужному макету.

0 голосов
/ 28 сентября 2019

Попробуйте этот метод, используя Grid Layout и AppcompatTextview, надеюсь, он вам поможет.

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

   <GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:rowCount="1">


          <androidx.appcompat.widget.AppCompatTextView
          android:id="@+id/text"
          android:layout_width="200dp"
          android:layout_height="wrap_content"
          android:layout_marginRight="10dp"
          android:ellipsize="end"
          android:autoSizeTextType="uniform"
          android:autoSizeMinTextSize="12sp"
          android:autoSizeMaxTextSize="100sp"
          android:autoSizeStepGranularity="2sp"
          android:maxLines="1"
          android:layout_columnWeight="0"
          android:text="Long text without button"
          android:textColor="#000"
          android:textSize="20sp" />

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

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