一起Talk Android吧(第一百四十五回:Android自定义View之Layout二)

各位看官们,大家好,上一回中咱们说的是Android中自定义View之Layout的例子,这一回咱们继续说该例子。闲话休提,言归正转。让我们一起Talk Android吧!

看官们,我们在上一回中介绍了View中的Layout细节和流程,而且分析了layout()方法,不过没有分析onLayout()方法,因为它是空的。当时也说了,View把它留给了子类去实现,我们在本章回中以View的子类TextView为例,来看看onLayout()方法具体做了些什么。好了,下面是源代码,请大家参考:

 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
      super.onLayout(changed, left, top, right, bottom);
      if (mDeferScroll >= 0) {
          int curs = mDeferScroll;
          mDeferScroll = -1;
          bringPointIntoView(Math.min(curs, mText.length()));
      }
      // Call auto-size after the width and height have been calculated.
      autoSizeText();
    }

从源代码中可以看到该方法比较简洁,不过核心的功能都在bringPointIntoView()方法中,TextView的位置就是通过它规划的,该方法内容比较多,具体的内容就不列出来了,大家可以自己去看看它的源代码。

各位看官,关于Androd中自定义View之Layout的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!

发布了520 篇原创文章 · 获赞 131 · 访问量 62万+

猜你喜欢

转载自blog.csdn.net/talk_8/article/details/100177847