自動換行佈局
阿新 • • 發佈:2018-11-14
package com.zbj.campus.relationship.custom_view; /** * Created by edz on 2018/1/19. */ import android.content.Context; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView;import com.zbj.campus.relationship.R; import java.util.ArrayList; import java.util.List; /** * 自動換行佈局 * Created by Went_Gone on */ public class WrapLayout extends ViewGroup { private static final String TAG = "WrapLayout"; /** * TextView的style */ public int TEXTVIEW_STYLE = 0; /** * Button的style */ public int BUTTON_STYLE = 1; private int style; private View btn; public WrapLayout(Context context) { super(context); } public WrapLayout(Context context, AttributeSet attrs) { super(context, attrs); } public WrapLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } /** * 設定資料 * @param data 文字 * @param context 上下文 * @param textSize 文字大小 * @param pl 左內邊距 * @param pt 上內邊距 * @param pr 右內邊距 * @param pb 下內邊距 * @param ml 左外邊距 * @param mt 上外邊距 * @param mr 右外邊距 * @param mb 下外邊距 */ public void setData(String[] data,Context context,int textSize,int pl,int pt,int pr,int pb,int ml,int mt,int mr,int mb){ createChild(data,context,textSize, pl, pt, pr, pb, ml, mt, mr, mb); } public void setData(List<String> data, Context context, int textSize, int pl, int pt, int pr, int pb, int ml, int mt, int mr, int mb){ String[] mydata = null; if(data!=null){ int length = data.size(); mydata = new String[length]; for(int i = 0 ; i<length;i++){ mydata[i] = data.get(i); } } setData(mydata,context, textSize,pl, pt, pr, pb, ml, mt, mr, mb); } private void createChild(String[] data, final Context context, int textSize, int pl, int pt, int pr, int pb, int ml, int mt, int mr, int mb){ int size = data.length; for(int i = 0;i<size;i++){ String text = data[i]; //通過判斷style是TextView還是Button進行不同的操作,還可以繼續新增不同的view if (style == TEXTVIEW_STYLE){ btn = new TextView(context); ((TextView) btn).setGravity(Gravity.CENTER); ((TextView) btn).setText(text); ((TextView) btn).setTextSize(textSize); }else if (style == BUTTON_STYLE){ btn = new Button(context); ((Button) btn).setGravity(Gravity.CENTER); ((Button) btn).setText(text); ((Button) btn).setTextSize(textSize); } btn.setClickable(true); btn.setPadding(dip2px(context, pl),dip2px(context, pt),dip2px(context, pr),dip2px(context, pb)); MarginLayoutParams params = new MarginLayoutParams(MarginLayoutParams.WRAP_CONTENT,MarginLayoutParams.WRAP_CONTENT); params.setMargins(ml, mt, mr, mb); btn.setLayoutParams(params); final int finalI = i; //給每個view新增點選事件 btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { markClickListener.clickMark(finalI); } }); this.addView(btn); } } private MarkClickListener markClickListener; public void setMarkClickListener(MarkClickListener markClickListener) { this.markClickListener = markClickListener; } public interface MarkClickListener{ void clickMark(int position); } /** * 根據手機的解析度從 dp 的單位 轉成為 px(畫素) */ private int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { return new MarginLayoutParams(getContext(), attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int childCount = getChildCount(); int lineWidth = 0; int lineHeight = 0; int width = 0;//warpcontet是需要記錄的寬度 int height = 0; for(int i = 0 ; i< childCount;i++){ View child = getChildAt(i); // 測量每一個child的寬和高 measureChild(child, widthMeasureSpec, heightMeasureSpec); MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); int childWidth = child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin; int childHeight = child.getMeasuredHeight()+lp.topMargin+lp.bottomMargin; // Log.e(TAG, "onMeasure: lineHeight = "+lineHeight+" childHeight = "+childHeight ); if(lineWidth+childWidth>widthSize){ width = Math.max(lineWidth, childWidth);//這種情況就是排除單個標籤很長的情況 lineWidth = childWidth;//開啟新行 height += lineHeight;//記錄總行高 lineHeight = childHeight;//因為開了新行,所以這行的高度要記錄一下 }else{ lineWidth += childWidth; // lineHeight = Math.max(lineHeight, childHeight); //記錄行高 lineHeight = Math.max(height, childHeight); //記錄行高 } // 如果是最後一個,則將當前記錄的最大寬度和當前lineWidth做比較 if (i == childCount - 1) { width = Math.max(width, lineWidth); //寬度 height += lineHeight; // } } setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? widthSize : width, (heightMode == MeasureSpec.EXACTLY) ? heightSize : height); /* int width1 = (widthMode == MeasureSpec.EXACTLY)? widthSize:width; int height1 = (heightMode == MeasureSpec.EXACTLY)? heightSize:height; Log.e(TAG, "onMeasure: widthSize ="+widthSize+" heightSize = "+heightSize ); Log.e(TAG, "onMeasure: width ="+width+" height = "+height ); Log.e(TAG, "onMeasure: widthEnd ="+width1+" heightEnd = "+height1 );*/ } /** * 儲存所有的View,按行記錄 */ private List<List<View>> mAllViews = new ArrayList<List<View>>(); /** * 記錄每一行的最大高度 */ private List<Integer> mLineHeight = new ArrayList<Integer>(); //onLayout中完成對所有childView的位置以及大小的指定 @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { mAllViews.clear(); //清空子控制元件列表 mLineHeight.clear(); //清空高度記錄列表 int width = getWidth();//得到當前控制元件的寬度(在onmeasure方法中已經測量出來了) int childCount = getChildCount(); // 儲存每一行所有的childView List<View> lineViews = new ArrayList<View>(); int lineWidth = 0; //行寬 int lineHeight = 0; //總行高 for(int i = 0 ; i<childCount;i++){ View child = getChildAt(i); child.setBackgroundResource(R.drawable.lib_campus_relationship_text_roun_stroke); MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();//得到屬性引數 int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); // 如果已經需要換行 if (i == 3){ i = 3; } if (childWidth + lp.leftMargin + lp.rightMargin + lineWidth > width) //大於父佈局的寬度 { // 記錄這一行所有的View以及最大高度 mLineHeight.add(lineHeight); // 將當前行的childView儲存,然後開啟新的ArrayList儲存下一行的childView mAllViews.add(lineViews); lineWidth = 0;// 重置行寬 lineViews = new ArrayList<View>(); } /** * 如果不需要換行,則累加 */ lineWidth += childWidth + lp.leftMargin + lp.rightMargin; lineHeight = Math.max(lineHeight, childHeight + lp.topMargin + lp.bottomMargin); lineViews.add(child); } // 記錄最後一行 (因為最後一行肯定大於父佈局的寬度,所以新增最後一行是必要的) mLineHeight.add(lineHeight); mAllViews.add(lineViews); int left = 0; int top = 0; int lineNums = mAllViews.size(); for(int i = 0;i<lineNums;i++){ // 每一行的所有的views lineViews = mAllViews.get(i); // 當前行的最大高度 每一行的高度都相同 所以使用(i+1)進行設定高度 lineHeight = (i+1)*mLineHeight.get(i); for(int j = 0 ;j < lineViews.size() ; j++){ View lineChild = lineViews.get(j); if(lineChild.getVisibility() == View.GONE){ continue; } MarginLayoutParams lp = (MarginLayoutParams) lineChild.getLayoutParams(); //開始畫標籤了。左邊和上邊的距離是要根據累計的數確定的。 int lc = left + lp.leftMargin; int tc = top+lp.topMargin; int rc = lc+lineChild.getMeasuredWidth(); int bc = tc+lineChild.getMeasuredHeight(); lineChild.layout(lc, tc, rc, bc); left += lineChild.getMeasuredWidth() + lp.rightMargin + lp.leftMargin; } left = 0;//將left歸零 top = lineHeight; } } public void setStyle(int style) { this.style = style; } }