Android 设置滚动视图的高度不起作用

Android set height to scrollview not work

我使用下面的代码为滚动视图的新高度

但是不行

viewFindPos = (ScrollView) findViewById(R.id.Roll);
viewFindPos.getLayoutParams().height = 5000;

// OR : viewFindPos.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 5000));

获取到ScrollView的Layout参数后,需要将LayoutParameters设置回View。稍后请求布局,因此它被重绘。查看下面的代码,这将帮助您理解。

LayoutParams params = viewFindPos.getLayoutParams();
params.height = 5000;
viewFindPos.setLayoutParams(params);
viewFindPos.requestLayout();

这个有效:

@Override

public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(有焦点); ScrollView viewFindPos = (ScrollView) findViewById(R.id.scrollView); viewFindPos.getLayoutParams().height = 500; } 或者

@Override

protected void onResume()

{
    super.onResume(hasFocus);
    ScrollView viewFindPos = (ScrollView) findViewById(R.id.scrollView);
    viewFindPos.getLayoutParams().height = 100;
}