Как не оттолкнуть ImageView от экрана с Long TextView? - PullRequest
0 голосов
/ 15 апреля 2019

Я хочу, чтобы мой просмотр изображений оставался справа от моего просмотра текста.Я могу сделать это, установив 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, который обнимает изображение в крайнем правом положении, мне нужно, чтобы оно было динамичным и всегда находилось справа от текста изатем, когда ширина текстового обзора становится больше, чем родительская, чтобы изображение не отталкивалось от экрана.

Ответы [ 3 ]

2 голосов
/ 15 апреля 2019

Вы можете использовать constraintLayout и установить ширину вашего textView на 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>

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

enter image description here

0 голосов
/ 15 апреля 2019

Я установил paddingRight на размер кнопки изображения внутри текстового обзора, и я использовал 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>
0 голосов
/ 15 апреля 2019

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...