0

我需要将边距设置为动态创建的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; 
     } 

我尝试过很多方法,但没有任何工程。目前,它不会按照给定的尺寸添加空间/保证金。

回答

0
if(layoutParams instanceof MarginLayoutParams) 
{ 
    ((MarginLayoutParams) layoutParams).topMargin = 15; 
    ((MarginLayoutParams) layoutParams).leftMargin = 15; 
    //... etc 
} 
+0

谢谢....让我试试 – VVB

+0

我认为从rafsanahm ..答案服务您的需要,因为我的解决方案只是为不能直接设置的元素设置边距。无论如何,我想你希望你的整个视图(scrollview包装linearlayout)与保证金。 –

0

需要调用scrollView.setLayoutParams(layoutParams);

还设置LayoutparamsScrollview和内蒙古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); 
+0

也许他想要在scrollview和linearlayout之间留有余量? ;) –

+0

谢谢....让我试试 – VVB

+0

没有为我工作。布局保持不变并且不会从任何一方添加保证金 – VVB

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; 
} 
+0

谢谢....让我试试 – VVB

+0

没有为我工作。布局保持不变并且不会从任何一侧添加保证金 – VVB

+0

您是否已将ScrollView添加到根视图? –

0

按本SO answer

scrollView.setFillViewport(true); 

这迫使childview伸展到父滚动视图。然后你可以设置边距,填充滚动视图后将增加空间

相关问题