Reputation: 614
I've extended the RelativeLayout class, and implemented all of the constructors as I think I need to. I've also added a child in the xml layout, just as I would with a regular RelativeLayout. Now, the problem is, I can't seem to access the children in the custom layout! Oddly, I can (in the graphical layout) select the child view and the custom layout is described as a 'parent', but no dice when trying to do it the other way around.
How do I 'tell' the class that it actually has children? Code below!
Thanks!
private void init(Context context) {
System.out.println(this.getChildCount()); // Always returns 0
}
public CustRLayout(Context context) {
super(context);
init(context);
}
public CustRLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustRLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
And the XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<app.hobi.CustRLayout android:id="@+id/cust"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/childView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/img"
android:src="@drawable/an_image" />
</app.hobi.CustRLayout>
<app.hobi.SomeOtherView android:id="@+id/other"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
Upvotes: 0
Views: 1254
Reputation: 6929
The children get added later (after call to constructor) via addChild()
. So query the childCount at a late state e.g.: in onMeasure() or use the ViewTreeObserver
Upvotes: 2