Question

I have a RelativeLayout defined in an xml layout file named 'myview.xml' which contains a number of sub-Views (TextViews, ImageViews etc).

I want to create a View object/class named 'MyView' that represents this RelativeLayout, which allows me to change the properties of the sub-Views using methods I will define and such that I can add (and retrieve) instances of MyView programmatically to (from) other Views.

What is the best way to do this? At the moment I am creating a class named 'MyView' which extends FrameLayout and in overriding FrameLayout's three constructors I call the following methods...

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.myview, null);
addView(view);

... This works but it means all my RelativeLayouts are contained within FrameLayouts and thus I have an extra (unnecessary) layer in my view hierarchy...

Was it helpful?

Solution

I do pretty much the same thing. But I send in 'this' rather than null and don't add that view to the parent view.

LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.myview, this);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top