MaterialCardView在以编程方式添加时不显示



以编程方式添加时,我无法在"活动"中看到MaterialViewCard。如果我直接在XML上添加,它会显示出来。但当通过Kotling添加时,它不会。

XML示例:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondFragment">
<LinearLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<LinearLayout
android:id="@+id/layout_first_block"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/surface"
android:layout_margin="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00"
android:textColor="@color/on_surface"/>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/surface"
android:layout_margin="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="01"
android:textColor="@color/on_surface"/>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/surface"
android:layout_margin="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="03"
android:textColor="@color/on_surface"/>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_weight="1">
<Button
android:id="@+id/button_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/previous"/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

结果:通过XML 添加的卡

但是,当我尝试这个(下面(是行不通的。XML:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondFragment">
<LinearLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<LinearLayout
android:id="@+id/layout_first_block"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<!--            Will add here-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_weight="1">
<Button
android:id="@+id/button_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/previous"/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Kotlin:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.button_second).setOnClickListener {
findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)
}
val layoutFirstBlock: LinearLayout = view.findViewById<LinearLayout>(R.id.layout_first_block)
for (r in 1..10){
val row: LinearLayout = LinearLayout(context)
row.orientation = LinearLayout.HORIZONTAL
row.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
for(c in 1..10){
val card: MaterialCardView = MaterialCardView(context)
val marginParams: ViewGroup.MarginLayoutParams = ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
marginParams.setMargins(R.dimen.small_padding, R.dimen.small_padding, R.dimen.small_padding, R.dimen.small_padding)
card.layoutParams = marginParams
card.setBackgroundColor(resources.getColor(R.color.surface, context?.theme))
val num: TextView = TextView(context)
num.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
val value = ((r-1)*10 + c).toString()
num.text = value
num.setTextColor(resources.getColor(R.color.on_surface, context?.theme))
Log.d("i", "i$r$c = $value")
card.addView(num)
row.addView(card)
}
layoutFirstBlock.addView(row, 0)
}
}

结果:Kotlin 添加时卡丢失

有什么想法吗?

谢谢!

在我的案例中,问题是我在卡片视图上方有一个组件,我将其高度设置为match_parent,当我将高度设置为wrap_content时,问题已经解决。

如果它不起作用,也检查下面的线的构建渐变。

implementation 'com.google.android.material:material:1.0.0'

首先

您还需要生成每个视图的id。

val card: MaterialCardView = MaterialCardView(context)
card.id = View.generateViewId()

val num: TextView = TextView(context)
num.id = View.generateViewId()

第二

我认为,你应该更新的索引

layoutFirstBlock.addView(row, 0)

layoutFirstBlock.addView(row, r)

最新更新