2016-08-28 4 views
0

シンプルなカスタムビューを作成しようとしていますが、問題があります。onMeasure()が正しく動作しません。 マイonMeasure()コード:onMeasure()には何の問題がありますか?

@Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     final int widthMode = MeasureSpec.getMode(widthMeasureSpec); 
     final int heightMode = MeasureSpec.getMode(heightMeasureSpec); 
     final int widthSize = MeasureSpec.getSize(widthMeasureSpec); 
     final int heightSize = MeasureSpec.getSize(heightMeasureSpec); 
     Log.d("ad","Width mode: "+widthMode+"\nHeight mode: "+heightMode); 
     int width = 0, height = 0; 
     textLayout = null; 

     switch (heightMode) { 
      case MeasureSpec.EXACTLY: 
       height = heightSize; 
       textLayout = new StaticLayout(textString, textPaint, height, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false); 
       //Log.d("ad","Checkpoint 1: "+height); 
       break; 
      case MeasureSpec.AT_MOST: 
       textLayout = new StaticLayout(textString, textPaint, heightSize, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false); 
       //Log.d("ad","Checkpoint 2: "+heightSize); 
       int lineCount = textLayout.getLineCount(); 
       if (lineCount > 0) { 
        if (lineCount==1){ 
         height = (int) textLayout.getLineWidth(0); 
        } else { 
         height = heightSize; 
        } 
       } 
       break; 
      case MeasureSpec.UNSPECIFIED: 
       textPaint.getTextBounds(textString, 0, textString.length(), textBounds); 
       height = textBounds.width(); 
       break; 
     } 

     switch (widthMode) { 
      case MeasureSpec.EXACTLY: 
       width = widthSize; 
       //Log.d("ad","Checkpoint 3!"); 
       break; 
      case MeasureSpec.AT_MOST: 
       if (textLayout!=null) { 
        int lineCount = textLayout.getLineCount(); 
        if (lineCount > 0) { 
         width = Math.min(textLayout.getLineBottom(lineCount-1), widthSize); 
         //Log.d("ad","Checkpoint 4: "+lineCount); 
        } 
       } else { 
        width = Math.min(textBounds.height(), widthSize); 
       } 
       break; 
      case MeasureSpec.UNSPECIFIED: 
       if (textLayout!=null) { 
        int lineCount = textLayout.getLineCount(); 
        if (lineCount > 0) { 
         width = textLayout.getLineBottom(lineCount-1); 
        } 
       } else { 
        width = textBounds.height(); 
       } 
       break; 
     } 

     setMeasuredDimension(width, height); 
    } 

これは私が今のオーバーライド唯一の方法です。これは、主に正常に動作しますが、私は(例えば)width=wrap_contentheight=200dpを使用している場合、それは幅間違って印刷次のログを計算します。

D/ad: Width mode: -2147483648 (AT_MOST) Height mode: -2147483648 (AT_MOST) 
D/ad: Width mode: 1073741824 (EXACTLY) Height mode: 1073741824 (EXACTLY) 

レイアウト:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <com.don.verticaltextview.VerticalTextView 
     android:layout_width="wrap_content" 
     android:layout_height="200dp" 
     android:textSize="24sp" 
     android:textColor="#111" 
     android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum" /> 

</RelativeLayout> 

しかしwidthMode=AT_MOSTであるonMeasure()コールとheightMode=EXACTLY

+0

なぜ2つのログ(通話)がonMeasureに届いていますか? heightSizeとwidthSize変数をログに追加してください。おそらく解決するのに役立ちます。 – lionscribe

+0

onMeasure()は毎回2回以上呼び出されたすべてのビューで(私には分かりません)、これは正常な動作ですが、間違ったmeasureSpecで呼び出されたときには奇妙です。 – don11995

答えて

0

私のカスタムビューの親であるRelativeLayoutに問題がありました。onMeasure()を2回、間違った測定仕様で呼び出しました。他のレイアウトでは、期待通りに動作し、onMeasure()は一度だけ呼び出されます。

関連する問題