1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| package com.cm.demo;
import android.content.Context; import android.text.Layout; import android.text.StaticLayout; import android.text.TextPaint; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;
public class CustomLayout extends ViewGroup { private static final int SINGLE_LINE = 0x01; private static final int MULTI_LINE = 0x02; private static final int NEXT_LINE = 0x03; private int type; private int lastLineTop; private float lastLineRight;
public CustomLayout(Context context) { super(context); }
public CustomLayout(Context context, AttributeSet attrs) { super(context, attrs); }
public CustomLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childCount = getChildCount(); int w = MeasureSpec.getSize(widthMeasureSpec); if (childCount == 2) { TextView tv = null; if(getChildAt(0) instanceof TextView){ tv = (TextView) getChildAt(0); initTextParams(tv.getText(), tv.getMeasuredWidth(), tv.getPaint()); }else{ throw new RuntimeException("CustomLayout first child view not a TextView"); }
View sencodView = getChildAt(1);
measureChildren(widthMeasureSpec, heightMeasureSpec);
if (tv.getMeasuredWidth() + sencodView.getMeasuredWidth() <= w) { int width = tv.getMeasuredWidth()+sencodView.getMeasuredWidth(); int height = Math.max(tv.getMeasuredHeight(), sencodView.getMeasuredHeight()); setMeasuredDimension(width, height); type = SINGLE_LINE; return; } if (getChildAt(0) instanceof TextView) { if (lastLineRight + sencodView.getMeasuredWidth() > w) { setMeasuredDimension(tv.getMeasuredWidth(), tv.getMeasuredHeight() + sencodView.getMeasuredHeight()); type = NEXT_LINE; return; }
int height = Math.max(tv.getMeasuredHeight(), lastLineTop + sencodView.getMeasuredHeight()); setMeasuredDimension(tv.getMeasuredWidth(), height); type = MULTI_LINE; } } else { throw new RuntimeException("CustomLayout child count must is 2"); }
}
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { if (type == SINGLE_LINE || type == MULTI_LINE) { TextView tv = (TextView) getChildAt(0); View v1 = getChildAt(1); tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight()); int left = (int) lastLineRight; int top = lastLineTop; int lastLineHeight = tv.getBottom()-tv.getPaddingBottom() -lastLineTop; if(v1.getMeasuredHeight() < lastLineHeight){ top = lastLineTop + (lastLineHeight - v1.getMeasuredHeight())/2; } v1.layout(left, top, left + v1.getMeasuredWidth(), top+v1.getMeasuredHeight()); } else if (type == NEXT_LINE) { View v0 = getChildAt(0); View v1 = getChildAt(1); v0.layout(0, 0, v0.getMeasuredWidth(), v0.getMeasuredHeight()); v1.layout(0, v0.getMeasuredHeight(), v1.getMeasuredWidth(), v0.getMeasuredHeight() + v1.getMeasuredHeight()); } }
private void initTextParams(CharSequence text, int maxWidth, TextPaint paint) { StaticLayout staticLayout = new StaticLayout(text, paint, maxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); int lineCount = staticLayout.getLineCount(); lastLineTop = staticLayout.getLineTop(lineCount - 1); lastLineRight = staticLayout.getLineRight(lineCount - 1); }
}
|