流式布局

流式布局:宽度是百分比宽度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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值