Android中实现标签自动换行方法二
public class FlowViewGroup extends ViewGroup {
private int mPaddingTop;
private int mPaddingLeft;
private int mPaddingRight;
private int mPaddingBottom;
private ArrayList< String> mList = new ArrayList < > ( ) ;
private Context mContext;
public FlowViewGroup ( Context context) {
super ( context) ;
mContext = context;
}
public FlowViewGroup ( Context context, AttributeSet attrs) {
super ( context, attrs) ;
mContext = context;
}
public FlowViewGroup ( Context context, AttributeSet attrs, int defStyleAttr) {
super ( context, attrs, defStyleAttr) ;
mContext = context;
}
@Override
protected void onLayout ( boolean changed, int l, int t, int r, int b) {
int top = 0 ;
int left;
int lineHeight = 0 ;
for ( int i = 0 ; i < lines. size ( ) ; i++ ) {
left = 0 ;
top += lineHeight;
lineHeight = 0 ;
ArrayList< View> views = lines. get ( i) ;
for ( View view : views) {
if ( view. getVisibility ( ) == GONE) {
continue ;
}
MarginLayoutParams layoutParams = ( MarginLayoutParams) view. getLayoutParams ( ) ;
view. layout ( left + layoutParams. leftMargin + mPaddingLeft, top + layoutParams. topMargin + mPaddingTop, left + view. getMeasuredWidth ( ) + layoutParams. leftMargin + mPaddingLeft, top + view. getMeasuredHeight ( ) + layoutParams. topMargin + mPaddingTop) ;
lineHeight = Math. max ( lineHeight, view. getMeasuredHeight ( ) + layoutParams. topMargin + layoutParams. bottomMargin) ;
left