gpt4 book ai didi

android - 重新排列 Framelayout android 上的 View 顺序

转载 作者:行者123 更新时间:2023-12-02 21:21:13 24 4
gpt4 key购买 nike

我在frameLayout上添加了五个 View 。

如何重新排列framelayout的childIndex。

我使用下面的代码:

fromindex  = 3;
toindex = 4;
View tempFrom = frameLayout.getChildAt(fromindex);
View tempTo = frameLayout.getChildAt(toindex);
frameLayout.removeViewAt(fromindex)
frameLayout.removeViewAt(toindex)
frameLayout.addView(tempFrom, toindex)
frameLayout.addView(tempTo,fromindex)

但它会引发以下错误。

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

如何重新排列framelayout的childindex?

最佳答案

how to re arrange the childIndex of framelayout.

FrameLayout 无法直接重新排列其子级,但您可以通过删除这些子级并将它们重新添加到正确的位置来实现。您的代码不起作用,因为您以不正确的顺序删除 View ,导致 View 仍然附加到父级:

fromindex  = 3;
toindex = 4;
View tempFrom = frameLayout.getChildAt(fromindex);
View tempTo = frameLayout.getChildAt(toindex);
// first remove the view which is above in the parent's stack
// otherwise, if you remove the other child you'll call the `removeViewAt`
// method with the wrong index and the view which was supposed to be detached
// from the parent is still attached to it
frameLayout.removeViewAt(toindex);
frameLayout.removeViewAt(fromindex);
// first add the child which is lower in the hierarchy so you add the views
// in the correct order
frameLayout.addView(tempTo,fromindex)
frameLayout.addView(tempFrom, toindex)

关于android - 重新排列 Framelayout android 上的 View 顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13819853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com