ScrollView内的LinearLayout不显示

时间:2017-12-31 11:08:41

标签: android scrollview

这是我的代码

mainView = new MainView(this, imageId, text, 3);
mainView.setOnTouchListener(this);
LinearLayout.LayoutParams mainParams = new 
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout mainLinear = new LinearLayout(this);
ScrollView.LayoutParams scrollParams = new 
ScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
ScrollView scrollView = new ScrollView(this);
LinearLayout.LayoutParams myViewParams = new 
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mainLinear.addView(mainView, myViewParams);

scrollView.addView(mainLinear, mainParams);
addContentView(scrollView, scrollParams);

当然它只在LinearLayout内有效。请告诉我如何将其放入ScrollView

1 个答案:

答案 0 :(得分:0)

ScrollView的孩子的身高只有WRAP_CONTENT。如果你使用xml AS尝试相同的布局会给你一个lint警告说明这一点。

如果您希望子视图在内容较少时占据滚动视图的完整高度,则可以使用setFillViewport()

这应该适合你:

mainView = new MainView(this, imageId, text, 3);
mainView.setOnTouchListener(this);
LinearLayout.LayoutParams mainParams = new 
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout mainLinear = new LinearLayout(this);
ScrollView.LayoutParams scrollParams = new 
ScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
ScrollView scrollView = new ScrollView(this);
LinearLayout.LayoutParams myViewParams = new 
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mainLinear.addView(mainView, myViewParams);

scrollView.addView(mainLinear, mainParams);
scrollView.setFillViewport(true);
addContentView(scrollView, scrollParams);