Не могу сверстать layout в android, что не так,?
Через RexyclerView вывожу элементы списка с следующим макетом каждого элемента:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="currency"
type="com.pet.moneyconvertor.room.CurrencyEntity" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@{currency.name}"
android:textSize="14pt"
tools:text="Рубль"
/>
<TextView
android:id="@+id/textCharCode"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@{currency.charCode}"
android:textSize="14pt"
android:layout_gravity="right"
tools:text="RUB"/>
</LinearLayout>
</layout>
Результат который я получаю
Как видно на скрине, правый TextView прижат к левому TextView. А левый не заполнил собой все пустое пространство.
При чем дизайнер показывает нормально, как мне и нужно
Ответы (2 шт):
Вы используете Weight для распределения элементов макета. Чтобы указывать в какой пропорции будут размещены ваши TextView нужно у каждого элемента указывать это поле:
android:layout_weight="..."
в вашем случае данное поле стоит только у первого текстового поля. Так же стоит учитывать что этот параметр начинает "работать" только когда у виджета ширина (высота) установлена на 0:
android:layout_width="0dp"
чтобы ваши виджеты не переполнили родительский макет стоит установить в нем сумму весов WeightSum:
android:weightSum="..."
В вашем случае можно поставить например сумму весов 4, где на наименование валюты будет выделено 3:
android:layout_weight="3"
а на код валюты 1:
android:layout_weight="1"
UPDATE
попробовал я полностью проэмулировать вашу ситуацию. Сделал адаптер для списка и загрузил в него демо данные, используя при этом разметку вашего элемента:
<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="wrap_content"
android:orientation="horizontal"
android:weightSum="10">
<TextView
android:id="@+id/textName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
android:background="@color/teal_200"
android:text="Рубль"
android:textColor="@color/black"
android:textSize="14pt" />
<TextView
android:id="@+id/textCharCode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_weight="3"
android:text="RUB"
android:textColor="@color/black"
android:textSize="14pt" />
</LinearLayout>
вот что показывает дизайнер в студии:
вот что показывает эмулятор:
есть и второй вариант разметки:
<androidx.constraintlayout.widget.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/textName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/teal_200"
android:text="Рубль"
android:textColor="@color/black"
android:textSize="14pt"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/guideline"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.85" />
<TextView
android:id="@+id/textCharCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RUB"
android:textColor="@color/black"
android:textSize="14pt"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/guideline"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
это то что регулирует распределение элементов:
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.85" />
вот результат:
я очень сомневаюсь что dataBinding ломает всю разметку)
Проверьте код onCreateViewHolder
LayoutInflater.from(parent.context), parent, false)




