Android 如何为动态创建的UI设置页边距
我需要将边距设置为动态创建的UI。我想给LinearLayout添加边距 下面是我的代码Android 如何为动态创建的UI设置页边距,android,android-layout,android-linearlayout,android-scrollview,Android,Android Layout,Android Linearlayout,Android Scrollview,我需要将边距设置为动态创建的UI。我想给LinearLayout添加边距 下面是我的代码 @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { viewPager = (ViewPager) conta
@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;
}
我试了很多方法,但都不管用。目前,它没有按照给定的维度添加空格/边距。您需要调用
scrollView.setLayoutParams(layoutParams)代码>
if(layoutParams instanceof MarginLayoutParams)
{
((MarginLayoutParams) layoutParams).topMargin = 15;
((MarginLayoutParams) layoutParams).leftMargin = 15;
//... etc
}
还将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);
如果要将边距添加到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
添加到布局中。
例如,在片段中(您使用的是getActivity()
,因此我假设您在片段中)
:
据此:
这将强制子视图拉伸到父scrollview。然后,您可以设置填充scrollview后将增加空间的边距。也许他想在scrollview和linearlayout之间留一个边距?;)对我不起作用。布局保持不变&不增加任何方面的利润我认为拉夫萨纳姆的答案是。。满足您的需要,因为我的解决方案只是为不能直接设置的元素设置边距。不管怎么说,我想你希望你的整个视图(滚动视图包裹线性布局)有边距。这对我来说不太合适。布局保持不变&没有从任何一侧添加边距您是否已将ScrollView添加到根视图?我的ScrollView是根视图view@VVB您是否已将其添加为rootView,并在片段#onCreateView()或活动#onCreate()中返回它?可能是错误的,但我在onCreateView()中返回linearLayout
@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;
}
scrollView.setFillViewport(true);