Programmatically using MATCH_PARENT for a RelativeLayout inside a FrameLayout
Asked Answered
P

1

6

I'm subclassing HorizontalScrollView (which is a FrameLayout) and adding a RelativeLayout to it programmatically.

The FrameLayout correctly fills the parent view, but RelativeLayout inside doesn't show up.

MainActivity::OnCreate()

setContentView(R.layout.activity_main); 
CustomHorizontalScrollView custom = new CustomHorizontalScrollView(this); 
ViewGroup contentView = (ViewGroup) findViewById(R.id.content); 
contentView.addView(custom,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 180));

CustomHorizontalScrollView::Constructor()

this.setBackgroundColor(Color.MAGENTA);
relativeLayout = new RelativeLayout(context);
relativeLayout.setBackgroundColor(Color.BLACK);
this.addView(relativeLayout, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

Result:

RelativeLayout not showing up

The RelativeLayout should be black and should fill parent but is not.

The weird thing is that if I specify width and height in pixels instead using MATCH_PARENT, the RelativeLayout appears.

this.addView(relativeLayout, new FrameLayout.LayoutParams(90, 90));

enter image description here

Does that mean that I can't use MATCH_PARENT when programmatically adding a RelativeLayout to a FrameLayout? Or am I missing something? Or maybe HorizontalScrollView only supports having a child with fixed width and height?

Proximo answered 16/2, 2014 at 20:50 Comment(1)
I've found out that calling relativeLayout.layout(l, t, r, b); in onLayout(boolean changed, int l, int t, int r, int b) makes the layout match parent. However, subviews do not align properly (ALIGN_PARENT_LEFT, ALIGN_PARENT_RIGHT).Proximo
A
8

I do not find strict info in Android SDK API Reference, but actually the HorizontalScrollView is a "Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display."
So, why do you need a HorizontalScrollView if its unique child must have the same width?
Likely, you can try the following:

this.addView(relativeLayout, new FrameLayout.LayoutParams(90, FrameLayout.LayoutParams.MATCH_PARENT));

... and the RelativeLayout will appear.
But maybe the following makes more sense:

this.addView(relativeLayout, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.MATCH_PARENT));

Then the RelativeLayout will be large enough to contain its children and you'll can scroll it using the HorizontalScrollView.
The HorizontalScrollView has the property fillViewport that you can set to true if you want that the HorizontalScrollView "stretch its content to fill the viewport": I think it can be useful only when the content is less large than the HorizontalScrollView, but (I repeat), if the content is ALWAYS less large than the HorizontalScrollView, then the HorizontalScrollView is useless.

Arnett answered 16/2, 2014 at 22:5 Comment(1)
Thanks for your reply. Actually, I wanted the RelativeLayout to fill the parent and then add a content view to the RelativeLayout that will have a greater viewport than the HorizontalScrollView. But I see your point, I think I'll have to find another way to achieve what I want. Thanks a lot!Proximo

© 2022 - 2024 — McMap. All rights reserved.