public class Myview extends ViewGroup {
public Myview(Context context) {
this(context,null);
}
public Myview(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public Myview(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//测量模式
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//测量ViewGroup的宽高
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
//ViewGroup的padding
int paddingTop = getPaddingTop();
int paddingBottom = getPaddingBottom();
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
//当前宽度
int curWidth = 0;
//当前高度
int curHeight = paddingTop + paddingBottom;
//最大宽度
int widthMax = 0;
for(int i = 0;i < getChildCount();){
View childView = getChildAt(i);
measureChild(childView, widthMeasureSpec, heightMeasureSpec);
MarginLayoutParams marginLayoutParams = (MarginLayoutParams) childView.getLayoutParams();
int childWidth = childView.getMeasuredWidth();
int childHeight = childView.getMeasuredHeight();
if(curWidth == 0 ){
curWidth = paddingLeft + paddingRight;
curHeight += marginLayoutParams.topMargin + marginLayoutParams.bottomMargin + childHeight;
}
if(curWidth + marginLayoutParams.rightMargin + marginLayoutParams.leftMargin + childWidth <= widthSize){
//不换行
curWidth += marginLayoutParams.rightMargin + marginLayoutParams.leftMargin + childWidth;
i++;
}else{
//换行
widthMax = Math.max(widthMax, curWidth);
curWidth = 0;
}
//判断是否是最后一行
if(i == getChildCount()){
widthMax = Math.max(widthMax, curWidth);
curHeight += marginLayoutParams.topMargin + marginLayoutParams.bottomMargin + childHeight;
}
}
setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : widthMax ,
heightMode == MeasureSpec.EXACTLY ? heightSize : curHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//获取ViewGroup的padding
int paddingTop = getPaddingTop();
int paddingBottom = getPaddingBottom();
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
//当前宽度;
int curWidth = paddingLeft;
//当前高度
int curHeight = paddingTop;
for(int i = 0; i < getChildCount();){
View childView = getChildAt(i);
MarginLayoutParams marginLayoutParams = (MarginLayoutParams) childView.getLayoutParams();
int childWidth = childView.getMeasuredWidth();
int childHeight = childView.getMeasuredHeight();
if(curWidth + paddingRight + marginLayoutParams.rightMargin + marginLayoutParams.leftMargin + childWidth <= getWidth()){
//不换行
childView.layout(curWidth + marginLayoutParams.leftMargin,
curHeight + marginLayoutParams.topMargin,
curWidth + marginLayoutParams.leftMargin + childWidth,
curHeight + marginLayoutParams.topMargin + childHeight);
curWidth += marginLayoutParams.leftMargin + childWidth + marginLayoutParams.rightMargin;
i ++;
}else{
//换行
curHeight += marginLayoutParams.topMargin + childHeight + marginLayoutParams.bottomMargin;
curWidth = paddingLeft;
}
}
}
}
//MainActivity里面的方法
private String mNames[] = { "完美世界", "大主宰", "斗破苍穹", "绝世唐门", "你好啊", "遮天", "微微一笑很倾城", "何以笙箫默", "三生三世十里桃花", "琅琊榜",
"天下", "刀剑神皇", "大漠谣", "回到明朝当王爷", "兵临天下", "谁的青春不迷茫" };
private void initChildViews() {
Myview mv=findViewById(R.id.mv);
ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
//设置两个控件之间的距离
lp.leftMargin = 10;
lp.rightMargin = 10;
lp.topMargin = 10;
lp.bottomMargin = 10;
for (int i = 0; i < mNames.length; i++) {
view = new TextView(this);
view.setText(mNames[i]);
view.setTextColor(Color.WHITE);
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.textshap));
mv.addView(view, lp);
final int finalI = i;
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,mNames[finalI],Toast.LENGTH_SHORT).show();
}
});
}
}
//textshap文件的代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#666666" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"
/>
</shape>