输入框流式布局(yyz勿看)

本文主要探讨了如何在Android开发中实现输入框的流式布局,详细解析了相关代码实现,帮助开发者理解并掌握这种布局方式。

//布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <EditText
            android:id="@+id/main_ed"
            android:layout_width="300dp"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确认" />
    </LinearLayout>


    <com.bway.day_0701_demo.FlowLayout
        android:id="@+id/flow"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </com.bway.day_0701_demo.FlowLayout>
</LinearLayout>


//流式布局代码

public class FlowLayout extends ViewGroup {


    public FlowLayout(Context context) {
        super(context);
    }

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

    public FlowLayout(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);
        //测量
        int widthSize=MeasureSpec.getSize(widthMeasureSpec);
        int widthMode=MeasureSpec.getMode(widthMeasureSpec);
        int heightSize=MeasureSpec.getSize(heightMeasureSpec);
        int heightMode=MeasureSpec.getMode(heightMeasureSpec);

        //定义属性
        int wight=0;
        int height=0;
        int childWidth =0;
        int  childHeight=0;
        int linWigth =0;
        int linHeight =0;
        int totaHeight =0;
        View viewChild;

        for (int i = 0; i <getChildCount() ; i++) {
            viewChild=getChildAt(i);
            //获取自布局宽高
            childWidth=viewChild.getMeasuredWidth();
            childHeight=viewChild.getMeasuredHeight();

            if (childWidth > widthSize){
                throw  new IllegalArgumentException("你的长度太大");
            }

            if (linWigth + childWidth>widthSize){
                //换行
                wight=widthSize;
                totaHeight +=linHeight;

                linWigth =childWidth;
                linHeight=childHeight;

            }else{
                //不换行
                linWigth+=childWidth;
                linHeight =Math.max(linHeight,childHeight);
                wight=Math.max(linWigth,wight);
            }
            if (i ==getChildCount()-1){
                totaHeight=  totaHeight+linHeight;
                height =totaHeight;
            }
        }
        wight =widthMode == MeasureSpec.EXACTLY?widthSize:wight;
        height=heightMode == MeasureSpec.EXACTLY?heightSize:height;

        setMeasuredDimension(wight,height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childWidth =0;
        int  childHeight=0;
        int linWigth =0;
        int linHeight =0;
        int totaHeight =0;
        View viewChild;

        for (int i = 0; i < getChildCount(); i++) {
            viewChild=getChildAt(i);
            childWidth=viewChild.getMeasuredWidth();
            childHeight=viewChild.getMeasuredHeight();

            if (linWigth +childWidth >getMeasuredWidth()){
                //换行
                linWigth=0;
                totaHeight +=linHeight;
                layoutView(viewChild,linWigth,totaHeight,linWigth+childWidth,totaHeight+childHeight);
                linHeight =childHeight;
                linWigth=childWidth;

            }else{
                //不换行
                layoutView(viewChild,linWigth,totaHeight,linWigth+childWidth,totaHeight+childHeight);
                linWigth+=childWidth;
                linHeight =Math.max(linHeight,childHeight);
            }
        }
    }

    private void layoutView(View viewChild, int linWidth, int totalHeight, int i, int i1) {

        linWidth+=getPaddingLeft();
        linWidth+=getPaddingTop();
        viewChild.layout(linWidth,totalHeight,i,i1);
    }
}


//MainActivity

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private Button btn;
    private FlowLayout flow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }

    private void initViews() {
        editText = findViewById(R.id.main_ed);
        btn = findViewById(R.id.btn);
        flow = findViewById(R.id.flow);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = editText.getText().toString();
                TextView textView = new TextView(MainActivity.this);
                textView.setText(s);
                flow.addView(textView);
            }
        });
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值