自定义RelativeLayout中子视图的索引

时间:2014-08-04 10:12:03

标签: android view android-relativelayout

我想知道如何绘制自定义RelativeLayout中的子视图,以便我可以使用getChildAt()(子视图在xml中定义)。

特别是,我的布局包含5个自定义布局,每个布局包含许多子视图。我看到的大多数解决方案似乎都在使用findViewById()indexOfChild(),但是,因为每个具有相同id的子视图会出现5次,所以我不确定是否可以实现此方法。作为旁注,我还想知道索引是如何确定的,这样当我修改自定义布局时,我就会知道如何调整索引。谢谢!

1 个答案:

答案 0 :(得分:1)

RelativeLayout不会覆盖getChildAt,因此订单是ViewGroup中定义的顺序。 getChildAt的代码是

2914    public View getChildAt(int index) {
2915        try {
2916            return mChildren[index];
2917        } catch (IndexOutOfBoundsException ex) {
2918            return null;
2919        }
2920    }