简体   繁体   English

以编程方式将MATCH_PARENT用于FrameLayout内的RelativeLayout

[英]Programmatically using MATCH_PARENT for a RelativeLayout inside a FrameLayout

I'm subclassing HorizontalScrollView (which is a FrameLayout) and adding a RelativeLayout to it programmatically. 我正在继承Horizo​​ntalScrollView(这是一个FrameLayout)并以编程方式向它添加RelativeLayout。

The FrameLayout correctly fills the parent view, but RelativeLayout inside doesn't show up. FrameLayout正确填充父视图,但内部的RelativeLayout不会显示。

MainActivity::OnCreate() 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() CustomHorizo​​ntalScrollView ::构造函数()

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没有显示出来

The RelativeLayout should be black and should fill parent but is not. RelativeLayout应该是黑色的,应该填充父级,但不是。

The weird thing is that if I specify width and height in pixels instead using MATCH_PARENT, the RelativeLayout appears. 奇怪的是,如果我使用MATCH_PARENT指定宽度和高度(以像素为单位),则会显示RelativeLayout。

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

在此输入图像描述

Does that mean that I can't use MATCH_PARENT when programmatically adding a RelativeLayout to a FrameLayout? 这是否意味着在以编程方式将RelativeLayout添加到FrameLayout时我无法使用MATCH_PARENT? Or am I missing something? 或者我错过了什么? Or maybe HorizontalScrollView only supports having a child with fixed width and height? 或者,Horizo​​ntalScrollView只支持具有固定宽度和高度的子项?

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." 我没有在Android SDK API Reference中找到严格的信息,但实际上Horizo​​ntalScrollView是一个“视图层次结构的布局容器,可以由用户滚动,允许它大于物理显示”。
So, why do you need a HorizontalScrollView if its unique child must have the same width? 那么,如果它的唯一子元素必须具有相同的宽度,为什么还需要Horizo​​ntalScrollView?
Likely, you can try the following: 可能,您可以尝试以下方法:

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

... and the RelativeLayout will appear. ...并且将出现RelativeLayout。
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. 然后RelativeLayout将足够大以包含其子项,您可以使用Horizo​​ntalScrollView滚动它。
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. Horizo​​ntalScrollView具有属性fillViewport,如果您希望Horizo​​ntalScrollView“拉伸其内容以填充视口”,则可以将其设置为true:我认为仅当内容不如Horizo​​ntalScrollView大时才有用,但(我重复) ,如果内容总是比Horizo​​ntalScrollView大,那么Horizo​​ntalScrollView就没用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM