Reputation: 2119
I want to extend a View to create a custom View, and automatically force some layout parameters, this way:
public class myView extends View {
public myView(Context context) {
this.setLayoutParameters(LayoutParameters(LayoutParameters.MATCH_PARENT, LayoutParameters.MATCH_PARENT));
}
}
My problem is, how can I set the LayoutParameters if I don't know if the view will be set inside a LinearLayout, a RelativeLayout or somewhere else? How can I detect which Layout type is the view in?
Upvotes: 1
Views: 47
Reputation: 60681
The layout parameters you're talking about all come from android.view.ViewGroup.LayoutParams
which is common to all the ViewGroup
classes (LinearLayout, RelativeLayout, etc.)
So you shouldn't need to distinguish between the parent view types; just use the LayoutParams
directly.
Upvotes: 2
Reputation: 2119
I just found an easy solution by myself. The question anyway remains valid as soon as there may be a easier and more complete way to do the same thing.
if (this.getParent() instanceof LinearLayout)
this.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
Upvotes: 1