在约束布局内以编程方式传播linearLayouts

时间:2019-03-08 17:40:08

标签: android android-linearlayout android-constraintlayout

我在通过编程将线性布局listItem添加到约束布局时遇到问题。我想要的是在约束布局中水平地均匀分布x数量的listItem。

约束布局包含一个线性布局@ + id / list,在其中以编程方式向其中添加视图listItem,但是行为并非我所期望的。

当我在运行时执行此操作时,似乎listItem设置了layout_width =“ wrap_content”并将所有添加的视图压缩到约束视图的左侧,并且它们之间没有空隙。

如果我在约束xml文件中添加几个listItem,它的行为将与预期的一样-将listItem均匀分布,以便它们填充约束视图的整个宽度。每个listItem都具有相同的空间。

我做错了什么?

约束布局:

import play.api.libs.json._

case class EmployerStoreDTO (id: String, storeName: String)

object EmployerStoreDTO {
  implicit val reads: Reads[EmployerStoreDTO] = Json.reads[EmployerStoreDTO]
}

import java.time.DayOfWeek

import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._

case class CompanyEmployerDTO(id: String,
                              companyName: String,
                              companyUrl: Option[String],
                              description: Option[String],
                              startDayOfWeek: DayOfWeek,
                              logoUrl: Option[String],
                              stores: List[EmployerStoreDTO] = List(),
                              supportHelpText: Option[String],
                              themeId: Option[String]) {
}

object CompanyEmployerDTO {
  implicit val reads: Reads[CompanyEmployerDTO] = (
    (JsPath \ "id").read[String] and
      (JsPath \ "companyName").read[String] and
      (JsPath \ "companyUrl").readNullable[String] and
      (JsPath \ "description").readNullable[String] and
      (JsPath \ "weekStartDay").read[Int].map(DayOfWeek.of) and
      (JsPath \ "logoUrl").readNullable[String] and
      (JsPath \ "stores").read[List[EmployerStoreDTO]] and
      (JsPath \ "supportHelpText").readNullable[String] and
      (JsPath \ "themeId").readNullable[String]
    ) (CompanyEmployerDTO.apply _)
}

import play.api.libs.json.Reads._
import play.api.libs.json._

case class CompanyEmployerCollectionDTO (companies: List[CompanyEmployerDTO])

object CompanyEmployerCollectionDTO {
  implicit val reads: Reads[CompanyEmployerCollectionDTO] =
    (JsPath \ "companies").read[List[CompanyEmployerDTO]].map(CompanyEmployerCollectionDTO.apply)
}

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

1 个答案:

答案 0 :(得分:0)

尝试将此行添加到您的商品中:

android:layout_width="fill_parent"

而不是:

    android:layout_width="0dp"

我希望它对您有用