android 获取控件真实高度

我们在android 的开发中,难免要计算下控件高度,来实现滑动一定距离隐藏显示某些控件的效果,如何正确获取高度?

ViewTreeObserver vto3 = flNotices.getViewTreeObserver(); //设置监听
vto3.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    public void  onGlobalLayout() {
        flNotices.getViewTreeObserver().removeGlobalOnLayoutListener(this);//移除监听

        scrollSonTitleHeight = scrollSonTitleHeight+flNotices.getMeasuredHeight();
        Logger.e("hight1:"+ scrollSonTitleHeight);

    }
});

这样就可以了,你可能会刚开始用这些方法:

类似 :getLayoutParams方法在获取固定宽高的时候可以使用  但是当你用了
android:layout_height="wrap_content"修饰以后就只能获取 -1 -2 此类值

  类似 :view.getHeight()   view.getMeasuredHeight(),当在onCreate()方法执行时候,还未完成正真的绘制,得到的结果只能是0

  

你可能感兴趣的:(android,日常问题,android)