首页 > 解决方案 > 当 LinearLayout 移动到另一个地方时,按钮丢失 onClickListener

问题描述

所以,我正在Android Studio中制作一个应用程序,遵循一些表格的命令。有一部分我必须开发动态 UI,所以我必须更改应用程序以创建灵活的横向布局。横向模式下的正常应用程序有 2 个垂直的主要 LinearLayout,每个权重为 0.5。

基本上,目标是检查左侧 LinearLayout “父亲”在横向模式下是否有足够的高度,以便在里面放置 3 个 LinearLayout “孩子”。如果没有,我必须将最后一个 LinearLayout 孩子传递给另一半 LinearLayout “父亲”。

我为此开发了这段代码:

val topLayout = findViewById<LinearLayout>(R.id.layoutTop)
val heightNeeded :Int = findViewById<LinearLayout>(R.id.linearLayoutRed).height * 3
if ( topLayout.height < heightNeeded ) {

    val blueLayout = findViewById<LinearLayout>(R.id.linearLayoutBlue)

    val secondHalf = findViewById<LinearLayout>(R.id.linearLayoutSecondHalf)
    val totalValueL = findViewById<LinearLayout>(R.id.linearLayoutResultingColor)

    topLayout.removeView(blueLayout)
    if (blueLayout.parent != null) {
        (blueLayout.parent as ViewGroup).removeView(blueLayout)
    }

    secondHalf.removeView(totalValueL)
    if (totalValueL.parent != null) {
        (totalValueL.parent as ViewGroup).removeView(totalValueL)
    }

    val paddTop = blueLayout.paddingTop
    blueLayout.setPaddingRelative(0,paddTop,0,0)

    secondHalf.addView(blueLayout)
    secondHalf.addView(totalValueL)

它工作并完成了我想要的结果,但是我移动到另一个 LinearLayout 的 LinearLayout 中的所有内容都失去了它们的活动。例如,在里面我有 2 个按钮正在调用一个函数,现在我无法点击它们,它无法识别它们。

请你帮助我好吗?谢谢

标签: androidandroid-studiokotlin

解决方案


推荐阅读