如何在Android中使用代码中的z索引

如何在Android中使用代码中的z索引,android,z-index,android-linearlayout,Android,Z Index,Android Linearlayout,我有一个线性布局,并动态添加视图项。我需要买新的东西,它们的位置比旧的低 例如: MenuLayout = (LinearLayout) MainActivity.findViewById (R.id.MenuLayout); MenuLayout.addView(newBox); //at the top MenuLayout.addView(newBox); //lower MenuLayout.addView(newBox); //At the bottom 在添加它们时,只需将它们添

我有一个线性布局,并动态添加视图项。我需要买新的东西,它们的位置比旧的低

例如:

MenuLayout = (LinearLayout) MainActivity.findViewById (R.id.MenuLayout);
MenuLayout.addView(newBox); //at the top
MenuLayout.addView(newBox); //lower 
MenuLayout.addView(newBox); //At the bottom

在添加它们时,只需将它们添加到集合中即可。然后可以通过集合引用对象的内容

private ArrayList<E> collection = new ArrayList<E>();

......

    MenuLayout = (LinearLayout) MainActivity.findViewById (R.id.MenuLayout);
    MenuLayout.addView(newBox); collection.add(newBox); //index 0 in collection
    MenuLayout.addView(newBox); collection.add(newBox); //index 1 in collection
    MenuLayout.addView(newBox); collection.add(newBox); //index 2 in collection 
private ArrayList collection=new ArrayList();
......
MenuLayout=(LinearLayout)MainActivity.findViewById(R.id.MenuLayout);
MenuLayout.addView(newBox);集合。添加(新箱)//集合中的索引0
MenuLayout.addView(newBox);集合。添加(新箱)//集合中的索引1
MenuLayout.addView(newBox);集合。添加(新箱)//集合中的索引2

首先,由于这是一个
线性布局
,因此您使用的是X轴或Y轴,而不是Z轴。用Android的术语来说,Z轴正从屏幕上朝着用户的眼睛走出来

另外,请不要多次尝试添加相同的小部件
newBox
,否则会崩溃


除此之外,您还可以使用
getChildCount()
getChildAt()
访问
ViewGroup
的子级,如
LinearLayout
。子项将根据它们添加的顺序编制索引。

不幸的是,我没有所有的项集。它们是动态创建和删除的。这样的事情没有理由会妨碍此方法的使用。如果它们是在循环中创建的,那么您可以在循环中添加它们。在代码中,它是不同的对象。