美文网首页
Android绘制基础

Android绘制基础

作者: endian | 来源:发表于2018-01-23 14:18 被阅读0次

Android绘制基础

Layout内部布局的自定义


  • 重写onMeasure()来计算内部布局

    • 调用每个子view的measure(),让子view自我测量
      • 可用空间的判断方法
        • EXACTLY/ATMOST:
          • 可用空间:MeasureSpec中的size
        • UNSPECFIED:
          • 无限大
    • 根据子view给出的尺寸,得出子view的位置,并保存他们的位置和尺寸
    • 根据子view的位置和尺寸计算出自己的尺寸,并用setMeasuredDimension()保存
  • 重写onLayout()来摆放子view

  • 案列:

      public class SomeLayout extends ViewGroup{
          ...
          @Override
          protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
              for (int i=0; i<getChildCount(); i++){
                  View childView =getChildAt(i);
                  LayoutParams lp = childView.getLayoutParams();
                  switch(lp.width){
                      default://开发者写的固定值
                          childWidthSpec = MeasureSpec.makeMeasureSpec(lp.width,EXACTLY);
                  }
              }
          }
      }
    

关于保存子view位置的两点说明


  1. 不是所有的Layout都需要保存子view的位置(因为有的Layout可以在布局阶段实时推导出子view的位置,例如LinearLayout);

相关文章

网友评论

      本文标题:Android绘制基础

      本文链接:https://www.haomeiwen.com/subject/sassaxtx.html