我需要为动态创建的UI设置边距。我想为LinearLayout添加保证金。
以下是我的代码
@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;
}
我尝试了很多方法但没有任何作用。目前,它没有根据给定的尺寸添加空间/边距。
答案 0 :(得分:0)
if(layoutParams instanceof MarginLayoutParams)
{
((MarginLayoutParams) layoutParams).topMargin = 15;
((MarginLayoutParams) layoutParams).leftMargin = 15;
//... etc
}
答案 1 :(得分:0)
您需要致电scrollView.setLayoutParams(layoutParams);
还将Layoutparams
设置为Scrollview
和内部LinearLayout
:
scrollView = new ScrollView(getActivity());
scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
scrollView.setFillViewport(true);
linearLayout = new LinearLayout(getActivity());
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) scrollView
.getLayoutParams();
layoutParams.setMargins(15, 15, 15, 15);
scrollView.setLayoutParams(layoutParams); //add this
scrollView.addView(linearLayout, layoutParams);
答案 2 :(得分:0)
如果要为LinearLayout
添加边距,则需要创建父级相同类型的LayoutParams
,所以:
scrollView = new ScrollView(getContext());
linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
ScrollView.LayoutParams layoutParams = new ScrollView.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
// I recommend you to set resolved size as margins, not pixels like you are doing here.
layoutParams.setMargins(15, 15, 15, 15);
scrollView.addView(linearLayout, layoutParams);
修改强>
请务必将ScrollView
添加到您添加LayoutParams
的布局中。
例如在Fragment
中(您正在使用getActivity()
,所以我想您在Fragment)
:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ScrollView scrollView = new ScrollView(getActivity());
LinearLayout 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.addView(linearLayout, layoutParams);
scrollView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return scrollView;
}
答案 3 :(得分:0)