【问题标题】:How to add children to GridLayout through code (dynamically)?如何通过代码(动态)将子级添加到 GridLayout?
【发布时间】:2020-12-23 04:47:37
【问题描述】:

在 GridLayout 里面我有 32 个非常相似的 FrameView,不同的是 ids(数字)

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/myFrame1">
        <ImageView
            android:id="@+id/myImg1"
            android:layout_width="190dp"
            android:layout_height="105dp"
            android:layout_margin="4dp"
            android:src="@drawable/myImg"
            android:contentDescription="@string/station1" />
        <TextView
            android:id="@+id/descTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="4dp"
            android:layout_marginBottom="34dp"
            android:background="@android:color/transparent"
            android:text="@string/Station 1"
            android:textColor="@android:color/black"
            android:textSize="16sp"
            android:textStyle="bold"
            android:layout_marginStart="4dp" />
    </FrameLayout>

稍后我可能会添加更多文本视图,但它开始变得非常混乱,所以我需要大致了解如何动态插入这种布局,最好是使用 Kotlin 语言。

【问题讨论】:

    标签: android xml android-layout kotlin android-gridlayout


    【解决方案1】:

    我创建了一个完整的项目,我在其中动态插入frame

    首先你必须创建布局frame_layout.xml。在此布局中粘贴:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/myFrame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <ImageView
            android:id="@+id/myImg"
            android:layout_width="190dp"
            android:layout_height="105dp"
            android:layout_margin="4dp"
            android:contentDescription="station1"
            android:src="@drawable/ic_launcher_foreground" />
    
        <TextView
            android:id="@+id/descTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginStart="4dp"
            android:layout_marginLeft="4dp"
            android:layout_marginBottom="34dp"
            android:background="@android:color/transparent"
            android:text="Station 1"
            android:textColor="@android:color/black"
            android:textSize="16sp"
            android:textStyle="bold" />
    </FrameLayout>
    

    在活动布局中:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView 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=".MainActivity"
        >
    
        <androidx.gridlayout.widget.GridLayout
            android:id="@+id/gridRoot"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:columnCount="2"
            app:rowCount="32"
            >
    
        </androidx.gridlayout.widget.GridLayout>
    
    </ScrollView>
    

    还有 MainActivity.kt:

    import android.os.Bundle
    import android.widget.ScrollView
    import androidx.appcompat.app.AppCompatActivity
    import kotlinx.android.synthetic.main.activity_main.*
    import kotlinx.android.synthetic.main.frame_layout.view.*
    
    class MainActivity : AppCompatActivity()
    {
        override fun onCreate(savedInstanceState: Bundle?)
        {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            for (i in (1..32))// numbers of frames
            {
                val frame = ScrollView(this) // create frame
                layoutInflater.inflate(R.layout.frame_layout, frame) // add layout to frame
    
                frame.tag = i.toString() //add tag to enable finding specific frame at runtime
    
                frame.descTextView.text = "Section $i" // change textView on frame ( use string resources! not hardcoded string like me)
    
                gridRoot.addView(frame) // add frame to grid 
            }
    
        }
    }
    

    结果:

    如果一切正常,您可以对其进行编辑,使其看起来像您想要的那样。

    【讨论】:

    • 效果很好,谢谢。请问我之后如何通过代码将它们全部删除。
    • I think this will help。如果您想删除所有 gridRoot 的所有子级并删除它们
    • 另外我在运行时找不到特定的帧。我尝试使用 frame.findViewWithTag("1") 但它似乎没有工作
    • 看来我只能访问带有最后一个标签的视图,而其他标签不起作用。
    • 奇怪:/你能迭代gridRoot中的每个视图并记录tag吗?
    猜你喜欢
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多