TextView не появляется, когда я добавляю его в LinearLayout - PullRequest
0 голосов
/ 23 июня 2019

Я пытаюсь создать cardView, который содержит изображение, и помимо этого изображения я хочу добавить две строки текста (вертикально наложенных друг на друга). Ниже приведен код, который я написал для этого (функция возвращает cardView). Тем не менее, в моем cardView ничего не появляется. Если я удаляю linearLayout и textViews, то я получаю карту, которая показывает imageView. Поэтому я предполагаю, что я делаю что-то не так с тем, как я устанавливаю linearLayout в моих textViews или как я добавляю мой linearLayout в tableRow.

 private fun constructCardView(header: String, info: String) : CardView {
    val cardView = CardView(this)

    cardView.setPaddingRelative(5,0,0,0)
    cardView.radius = 10F

    val tableLayout = TableLayout(this)
    val layoutParams = TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT, 1.0f)
    tableLayout.layoutParams = layoutParams

    val tableRow = TableRow(this)
    val tableRowParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0f)
    tableRow.layoutParams = tableRowParams


    val imageView = ImageView(this)
    imageView.setImageResource(R.drawable.ic_restaurant_black_24dp)
    imageView.minimumHeight = 10
    imageView.minimumWidth = 10

    val linearLayout = LinearLayout(this)
    val linearLayoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
    linearLayoutParams.setMargins(3,3,3,3)
    linearLayout.layoutParams = linearLayoutParams
    linearLayout.orientation = LinearLayout.VERTICAL

    val textViewHeader = TextView(this)
    val textViewHeaderLayoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
    textViewHeaderLayoutParams.setMargins(0, 0, 5, 0)
    textViewHeader.layoutParams = textViewHeaderLayoutParams
    textViewHeader.text = header
    textViewHeader.textSize = 20F

    val textViewInfo = TextView(this)
    val textViewInfoLayoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
    textViewInfoLayoutParams.setMargins(5, 0, 5, 0)
    textViewInfo.text = info
    textViewInfo.textSize = 12F
    textViewInfo.layoutParams = textViewInfoLayoutParams

    linearLayout.addView(textViewHeader)
    linearLayout.addView(textViewInfo)

    tableRow.addView(imageView)
    tableRow.addView(linearLayout)


    tableLayout.addView(tableRow)
    cardView.addView(tableLayout)
    cardView.requestLayout()

    return cardView
}

Любая помощь будет оценена.

1 Ответ

0 голосов
/ 23 июня 2019

Просто предложить альтернативу, если использование xml является опцией, так как я думаю, что это экономит много строк кода для выполнения той же работы.

Вы, вероятно, хотите использовать LinearLayout вместоTablelayout, чтобы сложить вещи вертикально или горизонтально, как более универсальный подход.Например, я использовал свойство layout_weight, чтобы определить, как строки делятся на проценты.

Создайте файл XML в папке layouts:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="200dp"
    android:layout_height="200dp"
    card_view:cardCornerRadius="4dp">

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

       <ImageView android:layout_width="0dp"
        android:layout_weight="0.4"
        android:layout_height="match_parent"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:orientation="vertical">

       <TextView
           android:id="@+id/header"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />

       <TextView
           android:id="@+id/info"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.v7.widget.CardView>

Затем в коде:

private fun constructCardView(header: String, info: String) : CardView {
    val cardView = LayoutInflater.from(this).inflate(R.layout.cardview, parent, false)
    cardView.header.text = header
    cardView.info.text = info
    return cardView as CardView
}
...