我需要将边距设置为动态创建的UI。我想为LinearLayout添加边距。如何将边距设置为动态创建的UI
下面是我的代码
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
viewPager = (ViewPager) container;
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
this.layoutInflater = inflater;
scrollView = new ScrollView(getActivity());
linearLayout = new LinearLayout(getActivity());
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(15, 15, 15, 15);
scrollView.setLayoutParams(layoutParams); //add this
scrollView.addView(linearLayout, layoutParams);
//adding few UI controllers dynamically by method call to here
return scrollView;
}
我尝试过很多方法,但没有任何工程。目前,它不会按照给定的尺寸添加空间/保证金。
谢谢....让我试试 – VVB
我认为从rafsanahm ..答案服务您的需要,因为我的解决方案只是为不能直接设置的元素设置边距。无论如何,我想你希望你的整个视图(scrollview包装linearlayout)与保证金。 –