When creating it in xml, the params layout_width and layout_height are required.
What about items created in Java code? What's their default layout?
How can I set their layout to fill_parent or wrap_content programmatically?
public void addView (View child) Since: API Level 1
Adds a child view. If no layout parameters are already set on the child, the default parameters for this ViewGroup are set on the child. Parameters child the child view to add See Also
generateDefaultLayoutParams()
Without additional parameters, addView(View)
uses the default parameters (mostly WRAP_CONTENT
). Looking at the source code,
protected LayoutParams [More ...] generateDefaultLayoutParams() {
if (mOrientation == HORIZONTAL) {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
} else if (mOrientation == VERTICAL) {
return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}
return null;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With