Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在应用程序启动时读取布局大小_Android_Android Layout_Android Scrollview - Fatal编程技术网

Android 在应用程序启动时读取布局大小

Android 在应用程序启动时读取布局大小,android,android-layout,android-scrollview,Android,Android Layout,Android Scrollview,我需要在活动启动期间检索布局xml文件中定义的ScrollView的高度。这是实现这一点的最佳实践。我尝试将代码放在onCreate()、onStart()和onResume()中。All在启动时给出的高度为0。 是否有类似于onFinishInflate()的方法来治疗活动性 这是我的密码: myScroll=(ScrollView) findViewById(R.id.ScrollView01); int height=myScroll.getHeight(); 执行布局后,需要查询视图边

我需要在活动启动期间检索布局xml文件中定义的
ScrollView
的高度。这是实现这一点的最佳实践。我尝试将代码放在
onCreate()
onStart()
onResume()中。All在启动时给出的高度为0。
是否有类似于
onFinishInflate()
的方法来治疗活动性

这是我的密码:

myScroll=(ScrollView) findViewById(R.id.ScrollView01);
int height=myScroll.getHeight();

执行布局后,需要查询视图边界。建议的位置是
onWindowFocusChanged(布尔值)
。有关更多信息,请参见此问题:

此外,如果您敢于扩展布局类,例如创建
LinearLayout
等的子类,则可以通过以下方式覆盖它的onLayout():

@Override
public void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);

    myScroll=(ScrollView) findViewById(R.id.ScrollView01);
    int height=myScroll.getHeight();
}

您可以这样做:

获取对ScrollView的最终引用(在onGlobalLayout()方法中访问)。接下来,从ScrollView获取ViewTreeObserver,并添加一个OnGlobalLayoutListener,覆盖onGLobalLayout并获取此listener中的测量值

final ScrollView myScroll = (ScrollView)findViewById(R.id.my_scroll);
ViewTreeObserver vto = myScroll.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        LayerDrawable ld = (LayerDrawable)myScroll.getBackground();
        height = myScroll.getHeight();
        width=myScroll.getHeight();
        ViewTreeObserver obs = myScroll.getViewTreeObserver();
        obs.removeOnGlobalLayoutListener(this);
    }

});
请参阅此线程中的更多内容:


谢谢@Anup。它起作用了。但是,
RemoveGlobalOnlyOutliner()
不推荐使用。有其他选择吗??