Программно распространять linearLayouts внутри макета ограничения - PullRequest
0 голосов
/ 08 марта 2019

У меня проблемы с программным добавлением linearlayouts, listItem, в макет ограничения.Я хочу равномерно распределить число x listItem по горизонтали в макете ограничения.

Макет ограничения содержит линейное расположение, @ + id / list, куда я добавляю представления listItem программно, но поведениене то, что я ожидаю.

Когда я делаю это во время выполнения, создается впечатление, что listItem установил layout_width = "wrap_content" и сжимает все мои добавленные представления слева от представления ограничения без воздуха между ними.

Если я добавлю несколько listItem в xml-файл ограничений, он будет работать как положено - равномерно распределить listItems, чтобы они заполнили всю ширину представления ограничения.Каждый listItem получает одинаковое количество места.

Что я сделал не так?

Макет ограничения:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<LinearLayout
    android:id="@+id/heading"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent">
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/na"
        android:textColor="@color/black"
        android:textSize="@dimen/text_size_normal"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
    <TextView
        android:id="@+id/wtu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/na"
        android:textColor="@color/black"
        android:textSize="@dimen/text_size_normal"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/emergency_drugname" />
</LinearLayout>
<LinearLayout
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/heading">
</LinearLayout>
</android.support.constraint.ConstraintLayout>

LinearLayout listItem

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listItem"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_weight="1">
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/na"
        android:textColor="@color/black"
        android:textSize="@dimen/text_size_normal"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/dose"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/na"
        android:textColor="#006600"
        android:textSize="@dimen/text_size_normal"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/total_dose"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/na"
        android:textColor="#006600"
        android:textSize="@dimen/text_size_normal"
        android:textStyle="bold" />
</LinearLayout>

1 Ответ

0 голосов
/ 08 марта 2019

попробуйте добавить эту строку в ваш товар:

android:layout_width="fill_parent"

вместо:

    android:layout_width="0dp"

Я надеюсь, что это сработает для вас

...