【发布时间】:2014-03-16 00:01:59
【问题描述】:
我正在继承 HorizontalScrollView(它是一个 FrameLayout)并以编程方式向它添加一个 RelativeLayout。
FrameLayout 正确填充了父视图,但里面的 RelativeLayout 没有显示出来。
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));
结果:
RelativeLayout 应该是黑色的并且应该填充父级,但不是。
奇怪的是,如果我以像素为单位指定宽度和高度,而不是使用 MATCH_PARENT,则会出现 RelativeLayout。
this.addView(relativeLayout, new FrameLayout.LayoutParams(90, 90));
这是否意味着我在以编程方式将 RelativeLayout 添加到 FrameLayout 时不能使用 MATCH_PARENT?还是我错过了什么?或者可能 HorizontalScrollView 只支持有一个固定宽度和高度的孩子?
【问题讨论】:
-
我发现调用 relativeLayout.layout(l, t, r, b); in onLayout(boolean changed, int l, int t, int r, int b) 使布局匹配父级。但是,子视图未正确对齐(ALIGN_PARENT_LEFT、ALIGN_PARENT_RIGHT)。
标签: android android-layout android-relativelayout android-framelayout fill-parent