流式布局:宽度是百分比宽度em %
固定布局:宽度是固定像素宽度px
将固定布局计算成流式布局公式: 目标元素 / 父元素 = 百分比宽度
public class AddDecreaseView extends ViewGroup {
public AddDecreaseView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//定义常量
int row = 0;
int dwidth = 18;
int width = getWidth();
int childCount = getChildCount();
for (int i = 0; i <childCount ; i++) {
View childAt = getChildAt(i);
int measuredWidth = childAt.getMeasuredWidth();
int measuredHeight = childAt.getMeasuredHeight();
if(dwidth+measuredWidth>width){
row++;
dwidth = 18;
}
childAt.layout(dwidth,measuredHeight*row,dwidth+measuredWidth,(row+1)*measuredHeight);
dwidth+=measuredWidth;
}
}
}
流式布局:网页中主要的划分区域的尺寸使用百分数(搭配min-*、max-*属性使用),例如,设置网页主体的宽度为80%,min-width为960px。图片也作类似处理(width:100%, max-width一般设定为图片本身的尺寸,防止被拉伸而失真)。
@OnClick(R.id.search)
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.search:
//获取输入框的值
String s = editText.getText().toString().trim();
final TextView textView = new TextView(MainActivity.this);
textView.setPadding(10,10,10,10);
textView.setBackgroundColor(R.drawable.shape);
textView.setText(s);
ttt.addView(textView);
editText.setText(null);
break;
}
}