3

frameLayout に 5 つのビューを追加しました。

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 を再配置するには?

4

3 に答える 3

0

このコードを試してください

frameLayout.removeView(tempFrom) //to remove views
frameLayout.removeView(tempTo)
于 2012-12-11T12:03:23.580 に答える
0

ビューを交換することはできません。新しいビュー グループを定義し、その親にインフレートして古いものを削除する必要があります。

View C = findViewById(R.id.C);
ViewGroup parent = (ViewGroup) C.getParent();
int index = parent.indexOfChild(C);
parent.removeView(C);
C = getLayoutInflater().inflate(optionId, parent, false);
parent.addView(C, index);
于 2012-12-11T12:42:48.660 に答える