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

Kotlin 以编程方式添加时不显示MaterialCardView,kotlin,programmatically-created,materialcardview,Kotlin,Programmatically Created,Materialcardview,以编程方式添加时,我无法在活动中看到MaterialViewCard。如果我直接在XML上添加,它会显示。但当通过Koling添加时,它不会 XML示例: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/re

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

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:


有什么想法吗


谢谢

不确定您的问题,但建议调试。1) 使用层次结构查看器确认视图已正确添加2)使用“显示布局边界”调试选项以确认视图是否正确布局3)在XML中声明子视图并对其进行膨胀,而不是以编程方式进行4)考虑使用GRIDLAUDFEAM管理器来使用RealdReVIEW。谢谢你,伙计!
<?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>
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)
        }
    }