我有这个简洁的功能:
private void addMapFragment(){
if(!mapFragment.isAdded()){
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.mapContainer, mapFragment);
ft.commit();
}
}
Run Code Online (Sandbox Code Playgroud)
我正在打电话给addMapFragment()我的活动onCreate().然后,我从一个调用的webrequest进行回调addMapMapFragment().这个isAdded()方法看起来并没有什么用处,因为我遇到了一个崩溃说"片段已经添加:MapFragment[...]"
任何线索?
我正在继承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));
Run Code Online (Sandbox Code Playgroud)
CustomHorizontalScrollView ::构造函数()
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));
Run Code Online (Sandbox Code Playgroud)
结果:

RelativeLayout应该是黑色的,应该填充父级,但不是.
奇怪的是,如果我使用MATCH_PARENT指定宽度和高度(以像素为单位),则会显示RelativeLayout.
this.addView(relativeLayout, new FrameLayout.LayoutParams(90, 90));
Run Code Online (Sandbox Code Playgroud)

这是否意味着在以编程方式将RelativeLayout添加到FrameLayout时我无法使用MATCH_PARENT?或者我错过了什么?或者,HorizontalScrollView只支持具有固定宽度和高度的子项?
提前致谢,
android relativelayout android-layout fill-parent android-framelayout