布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rel_table_root" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/dp_12_size" android:layout_marginRight="@dimen/dp_12_size" android:background="@drawable/conner_bg_class_table" android:orientation="vertical" android:gravity="center"> <LinearLayout android:id="@+id/ll_top" android:layout_width="match_parent" android:layout_height="@dimen/dp_90_size" android:background="@mipmap/table_top" android:orientation="vertical"> </LinearLayout> <RelativeLayout android:id="@+id/rel_table" android:background="@drawable/table" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center|top"> <com.zpeducation.custview.FixGridLayout android:background="@color/white" android:id="@+id/ll_date" android:layout_width="@dimen/dp_882_size" android:layout_height="wrap_content" android:layout_marginTop="@dimen/dp_16_size" android:orientation="horizontal"></com.zpeducation.custview.FixGridLayout> </RelativeLayout> </LinearLayout>
activity中的使用
设置控件内每个view的宽和高
width = ScreenUtil.dip2px(this,49); height = ScreenUtil.dip2px(this,49); fixLayout.setmCellHeight(height); fixLayout.setmCellWidth(width);自动补齐,每行显示不全的自动补齐
int index; if(list.size()<=6){ index=6; }else{ index =(list.size()/6+1)*6; } for (int i = 0; i <index;i++) { if(i<list.size()){ createClassTable(i+"",list.get(i),list.size()); }else{ createClassTable(i+"",null,list.size()); } } int relWidth=mScreenWidth-ScreenUtil.dip2px(this,14); int relHeight=height*index/6+ScreenUtil.dip2px(this,50); LinearLayout.LayoutParams viewParams = new PercentLinearLayout.LayoutParams(relWidth,relHeight); relTable.setLayoutParams(viewParams);重点:绘制每个view,并把每个view添加到第三方的类中
private void createClassTable(String i, final GradeAdjustLessonsListBean.ScheduleListBean content, final int size) { final int number = Integer.parseInt(i); /* int width = ScreenUtil.dip2px(this,49); int height = ScreenUtil.dip2px(this,49); fixLayout.setmCellHeight(height); fixLayout.setmCellWidth(width);*/ final LinearLayout linearLayout = new LinearLayout(this); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height); linearLayout.setGravity(Gravity.CENTER_HORIZONTAL); linearLayout.setOrientation(LinearLayout.VERTICAL); linearLayout.setLayoutParams(params); TextView textView = new TextView(this); if(content==null){ textView.setText(""); }else{ textView.setText(content.getDate()); } textView.setWidth(width); textView.setHeight(ScreenUtil.dip2px(this,17)); textView.setGravity(Gravity.CENTER); textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, ScreenUtil.dip2px(this,11)); textView.setTextColor(getResources().getColor(R.color.white)); textView.setBackgroundColor(getResources().getColor(R.color.calendar_bg)); final TextView textViewTime = new TextView(this); if(content==null){ textViewTime.setText(""); }else{ if(content.getCanChoose()==0){ textViewTime.setTextColor(getResources().getColor(R.color.text_gray)); textViewTime.setText(content.getSort()); }else{ textViewTime.setTextColor(getResources().getColor(R.color.calendar_text)); textViewTime.setText(content.getSort()); } } textViewTime.setGravity(Gravity.CENTER); textViewTime.setWidth(width); textViewTime.setHeight(ScreenUtil.dip2px(this,32)); textViewTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, ScreenUtil.dip2px(this,20)); if(number==5|| number==11 ||number==17||number==23||number==29){ textViewTime.setBackgroundResource(R.drawable.border_left_right_blue); }else{ textViewTime.setBackgroundResource(R.drawable.border_left_blue); } linearLayout.addView(textView); linearLayout.addView(textViewTime); linearLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(number<size){ if(content.getCanChoose()==1){ stateList.get(number).setCheck(!stateList.get(number).isCheck); if(stateList.get(number).isCheck){ linearLayout.setBackgroundResource(R.color.text_gray); }else{ linearLayout.setBackgroundResource(R.color.transparent); } showBtnNext(size,content.getCanChoose()); } } } }); fixLayout.addView(linearLayout); }
引用的第三方的类
package com.zpeducation.custview; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import com.zpeducation.R; import com.zpeducation.main.BaseApplication; import com.zpeducation.utils.ScreenUtil; /** * Created by Administrator on 2017/1/4. */ public class FixGridLayout extends ViewGroup { private int mCellWidth; private int mCellHeight; public FixGridLayout(Context context) { super(context); } public FixGridLayout(Context context, AttributeSet attrs) { super(context, attrs); } public FixGridLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void setmCellWidth(int w) { mCellWidth = w; requestLayout(); } public void setmCellHeight(int h) { mCellHeight = h; requestLayout(); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int cellWidth = mCellWidth; int cellHeight = mCellHeight; int columns = (r - l) / cellWidth; if (columns < 0) { columns = 1; } int x = 0; int y = 0; int i = 0; int count = getChildCount(); for (int j = 0; j < count; j++) { final View childView = getChildAt(j); // 获取子控件Child的宽高 int w = childView.getMeasuredWidth(); int h = childView.getMeasuredHeight(); // 计算子控件的顶点坐标 int left = x + ((cellWidth - w) / 2); int top = y + ((cellHeight - h) / 2); // int left = x; // int top = y; // 布局子控件 childView.layout(left, top, left + w, top + h); if (i >= (columns - 1)) { i = 0; x = 0; y += cellHeight; } else { i++; x += cellWidth; } } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 创建测量参数 int cellWidthSpec = MeasureSpec.makeMeasureSpec(mCellWidth, MeasureSpec.AT_MOST); int cellHeightSpec = MeasureSpec.makeMeasureSpec(mCellHeight, MeasureSpec.AT_MOST); // 记录ViewGroup中Child的总个数 int count = getChildCount(); // 设置子空间Child的宽高 for (int i = 0; i < count; i++) { View childView = getChildAt(i); childView.measure(cellWidthSpec, cellHeightSpec); } // 设置容器控件所占区域大小 // 注意setMeasuredDimension和resolveSize的用法 int heightCount; if(count%6==0){ heightCount = count/6; }else{ heightCount = count/6+1; } setMeasuredDimension(resolveSize(mCellWidth * count, widthMeasureSpec), resolveSize(mCellHeight * heightCount, heightMeasureSpec)); // setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); // 不需要调用父类的方法 // super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void dispatchDraw(Canvas canvas) { // 获取布局控件宽高 int width = getWidth(); int height = getHeight(); // 创建画笔 Paint mPaint = new Paint(); // 设置画笔的各个属性 mPaint.setColor(getResources().getColor(R.color.transparent));//边框颜色 mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(ScreenUtil.dip2px(BaseApplication.getInstance(),0)); mPaint.setAntiAlias(true); // 创建矩形框 Rect mRect = new Rect(0, 0, width, height); // 绘制边框 canvas.drawRect(mRect, mPaint); // 最后必须调用父类的方法 super.dispatchDraw(canvas); } }