流式布局简单写法

//在activity中书写的内容

//获取输入框的文本
Editable text = flow_view.getText();
TextView textView = new TextView(MainActivity.this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(100, 10, 10, 10);//4个参数按顺序分别是左上右下
//textView.setLayoutParams(layoutParams);
textView.setText(flow_view.getText().toString());

//添加一个新的视图
hestoryview.addView(textView);
//searchHistory.measure(100,100);



//自定义View中的内容
package com.example.app_mounth.view.controlview;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class HestoryView extends ViewGroup {

private int sizeScreenWidth;

public HestoryView(Context context) {
this(context,null);
}

public HestoryView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}

public HestoryView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);
sizeScreenWidth = MeasureSpec.getSize(widthMeasureSpec);
}


@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int left = 0;
int top = 0;
int marginHorizontal = 20;
for (int i = 0; i < getChildCount(); i++) {
View childAt = getChildAt(i);
int measuredWidth = childAt.getMeasuredWidth();
int measuredHeight = childAt.getMeasuredHeight();
if (left + measuredWidth >= sizeScreenWidth) {
top += measuredHeight;
left = 0;
}
childAt.layout(left,top,left + measuredWidth, top + measuredHeight);
left += measuredWidth + marginHorizontal;
}
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}

猜你喜欢

转载自www.cnblogs.com/fybb/p/11250520.html
今日推荐