Как не оттолкнуть ImageView от экрана с Long TextView?

1

Я хочу, чтобы мой просмотр изображений оставался справа от моего просмотра текста. Я могу сделать это, установив toLeftOf в атрибутах для TextView. Но для действительно длинных текстовых строк я установил эллипсы, и после того, как он достигнет ширины макета, он также вытолкнет ImageView.

Я могу отобразить ImageView, установив гравитацию в 1 для TextView, но затем ImageView будет привязан вправо. Что я должен делать?

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

    <TextView
        android:id="@+id/title_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:layout_weight="1"
        android:text="MY TEXT TO THE LEFT"
        android:layout_toLeftOf="@id/img_button" />

    <ImageButton
        android:id="@+id/img_button"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="6dp"
        android:background="@drawable/some_img"        
        app:srcCompat="@drawable/ic_edit" />

</LinearLayout>

Таким образом, с установкой веса = 1, который обнимает изображение в крайнем правом положении, мне нужно, чтобы оно было динамичным и всегда находилось справа от текстового представления, а затем, когда ширина текстового представления становится больше, чем родительский, для изображение не будет отталкиваться от экрана.

Теги:
android-layout

3 ответа

2

Вы можете использовать constraintLayout и установить ширину 0dp (также называемой match constraint).

Результат заставит ваш textView отбросить строку, если текста много.

Вот пример:

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


    <ImageView
        android:id="@+id/textView8"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:background="@android:color/holo_green_light"
        android:padding="10dp"
        android:text="something"
        android:textColor="@color/colorAccent"
        app:layout_constraintBottom_toBottomOf="@+id/textView10"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline3"
        app:layout_constraintTop_toTopOf="@+id/textView10" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
       app:layout_constraintGuide_percent="0.5" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="this is really long text and it wont push the green image because of   android:layout_width attribute"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline3"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

И это будет выглядеть так:

Изображение 174551

0

Я установил paddingRight равным размеру кнопки изображения внутри textview, и я использовал alignRight на своей кнопке изображения. И изменил контейнер на RelativeLayout. Работает как задумано !!

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="left">

    <TextView
        android:id="@+id/title_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:paddingRight="22dp"
        android:text="MY TEXT TO THE LEFT"
        android:layout_toLeftOf="@id/img_button" />

    <ImageButton
        android:id="@+id/img_button"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="6dp"
        android:layout_alignRight="@+id/title_view"
        android:background="@drawable/some_img"        
        app:srcCompat="@drawable/ic_edit" />

</RelativeLayout>
-1

Используйте LinearLayout с LinearLayout orientation="horizontal". Чем установить weight_sum = 100 для LinearLayout, а чем для ваших TextView и ImageView, просто поместите атрибут веса в соответствии с тем, что вы хотите. Надеюсь, поможет.

  • 0
    Это все еще отталкивает ImageView от.
  • 0
    установить вес как изображения, так и текста

Ещё вопросы

Сообщество Overcoder
Наверх
Меню