我以编程方式创建并添加片段到视图 这是我的代码:
CollectionsSlideShowFragment fragment = new CollectionsSlideShowFragment();
Bundle bundle = new Bundle();
bundle.putString("collections", gson.toJson(collectionsSlideShow));
fragment.setArguments(bundle);
FrameLayout view = new FrameLayout(this);
addFragment(R.id.collectionsSlideshow, fragment);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
view.setMargins(0, (int) UiUtils.dp2px(15), 0, 0);
view.setId(R.id.collectionsSlideshow);
contentContainer.addView(view, params);
protected Fragment addFragment(int container, Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(container, fragment);
fragmentTransaction.commit();
return fragment;
}
但是在framelayout中添加边距没有任何效果,我已经对SO建议设置重力的其他答案进行了评论,但这也没有任何效果。
如何将边距设置为片段?