閱讀497 返回首頁    go 阿裏雲 go 技術社區[雲棲]


自定義組件及其內組件大小的正確設置

https://blog.csdn.net/xiaodao1986/article/details/8481288

一、自定義組件大小的設置

不管你怎麼設置,自定義組件似乎總是match_parent。我們需要覆蓋onMeasure方法,並在其內正確設置組件大小。

[java] view plaincopy
  1. @Override  
  2. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  3.     //注意:為盡可能簡潔本文隻討論EXACTLY和AT_MOST模式  
  4.     int mode = MeasureSpec.getMode(widthMeasureSpec);  
  5.     if(mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) {  
  6.         this.widthMeasureSpec = widthMeasureSpec;  
  7.         this.heightMeasureSpec = heightMeasureSpec;  
  8.         int width = MeasureSpec.getSize(widthMeasureSpec);  
  9.         int height = MeasureSpec.getSize(heightMeasureSpec);  
  10.         setMeasuredDimension(width, height);  
  11.     } else if(mode == MeasureSpec.UNSPECIFIED) {  
  12.         Log.d("WOGU""mode=UNSPECIFIED");  
  13.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  14.     }  
  15. }  


最後更新:2017-04-04 07:03:34

  上一篇:go HDU3579 一元線性同餘方程
  下一篇:go 最近發現了一個高清計算機電子書和源碼的網址