1 通过 looper 队列

    /**     * 通过 looper 队列     */    private void getHeight1() {        tvText.post(new Runnable() {            @Override            public void run() {                int height = tvText.getHeight();                Log.i(TAG, "MainActivity;getHeight1;height=" + height);            }        });    }

2  当视图树的布局发生改变时

    /**     * 当视图树的布局发生改变时     */    private void getHeight2() {        tvText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {            @Override            public void onGlobalLayout() {                tvText.getViewTreeObserver().removeOnGlobalLayoutListener(this);                int height = tvText.getHeight();                Log.i(TAG, "MainActivity;getHeight2;height=" + height);            }        });    }

3  当视图树的进行绘制时

    /**     * 当视图树的进行绘制时     */    private void getHeight3() {        tvText.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {            @Override            public boolean onPreDraw() {                tvText.getViewTreeObserver().removeOnPreDrawListener(this);                int height = tvText.getHeight();                Log.i(TAG, "MainActivity;getHeight3;height=" + height);                return false;            }        });    }

4 view 布局改变时

    /**     * view 布局改变时     */    private void getHeight4() {        tvText.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {            @Override            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {                tvText.removeOnLayoutChangeListener(this);                int height = tvText.getHeight();                Log.i(TAG, "MainActivity;getHeight4;height=" + height);            }        });    }

 

 

更多相关文章

  1. 大话企业级Android应用开发实战 用户界面
  2. Android(安卓)GridView设置条目的高度与宽度相等、GridView条目
  3. Android(安卓)DataBinding ViewPage + Fragment 封装
  4. Android(安卓)dialog 对话框自定义布局
  5. Android学习笔记:TabHost 和 FragmentTabHost
  6. Android程序中输入法弹出的时候如何不遮挡输入,与布局相配
  7. 2019年最新Android常用开源库汇总上篇(持续更新)

随机推荐

  1. [Android(安卓)调试/测试] Android(安卓)
  2. Android入门篇六:使用意图传递数据之返回
  3. android修炼大法
  4. Android异步处理系列文章索引
  5. Android(安卓)客户端与服务器交互方式
  6. Android下生成core dump的方法
  7. 如何在Android真机上检测是否有Google Ma
  8. Android画图之抗锯齿
  9. Android(安卓)adb的使用略解
  10. 硬件访问服务4之Android硬件访问服务框架