如何将边距设置为动态创建的UI

时间:2017-04-22 18:35:46

标签: android android-layout android-linearlayout android-scrollview

我需要为动态创建的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;
        }

我尝试了很多方法但没有任何作用。目前,它没有根据给定的尺寸添加空间/边距。

4 个答案:

答案 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)

根据SO answer

null

这会强制子视图延伸到父卷轴视图。然后,您可以设置在填充滚动视图后将添加空间的边距。