简单的流式布局

一、自定义控件

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
public class CustomView extends ViewGroup {

    private int mleftMArgin =20;
    private int mtopMargin = 20;

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

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

    public CustomView(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 leftMargin = mleftMArgin;
        int topMargin = mtopMargin;

        int viewHeight = 0;
        int viewWidth =0;

        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeVidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizHeight= MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        switch (modeHeight){
            case MeasureSpec.AT_MOST:
                int measuredHeight = 0;
                for (int j=0;j<getChildCount();j++){
                    int measuredWidth = getChildAt(j).getMeasuredWidth();
                    measuredHeight = getChildAt(j).getMeasuredHeight();
                    if (leftMargin+measuredWidth+mleftMArgin>getWidth()){
                        leftMargin =mleftMArgin;
                        topMargin+=measuredHeight+mtopMargin;
                    }
                    leftMargin+=measuredWidth+mleftMArgin;
                }
                topMargin+=measuredHeight+mtopMargin;
                break;
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        int leftMargin=mleftMArgin;
        int topMargin=mtopMargin;
        for (int j=0;j<getChildCount();j++){
            int measuredwidth = getChildAt(j).getMeasuredWidth();
            int measuredHeight = getChildAt(j).getMeasuredHeight();
            if (leftMargin+measuredwidth+mleftMArgin>getWidth()){
                leftMargin=mleftMArgin;
                topMargin+=measuredHeight+mtopMargin;
                getChildAt(j).layout(leftMargin,topMargin,
                leftMargin+measuredwidth,
                topMargin+measuredHeight);
            }else {
                getChildAt(j).layout(leftMargin,topMargin,
                        leftMargin+measuredwidth,
                        topMargin+measuredHeight);
            }
            leftMargin+=measuredwidth+mleftMArgin;
        }

    }
}

二、布局

<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="com.bwei.fangguow.activity.MainActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:background="@null">
        <Button
            android:id="@+id/result_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="搜索"
            android:textSize="14sp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"/>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="20dp"
            android:layout_toLeftOf="@id/result_search"
            android:background="@drawable/addedit_bg"
            android:focusable="true"
            android:focusableInTouchMode="true">
            <EditText
                android:id="@+id/shuru"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="输入内容"/>
        </RelativeLayout>
    </RelativeLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#dddddd"/>
    <Button
        android:id="@+id/qingkong"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清空历史记录"/>
    <com.bwei.fangguow.liushi.CustomView
        android:id="@+id/mSearchFlowLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></com.bwei.fangguow.liushi.CustomView>
</LinearLayout>

三、MainActivity.java

public class MainActivity extends AppCompatActivity{

    String[] name={"shuju1","shuju2","shuju3","shuju4","shuju5",
            "shuju6","shuju7","shuju8","shuju9","shuju10"};

    private Button sousuo;
    private EditText shuru;
    private CustomView mSearchlinFlowLayout;
    private TextView textView;
    private Button jinru;
    private Button qingkong;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initView();
        //展示数据
        intiData();
        //点击删除
        qingkong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSearchlinFlowLayout.removeAllViews();
            }
        });
    }

    private void intiData() {
        //展示数据
        for (int i = 0; i < name.length; i++) {
            textView = new TextView(this);
            textView.setText(name[i]);
            textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.addedit_bg));
            textView.setPadding(5,5,5,5);
            textView.setTextSize(20);
            textView.setTextColor(Color.BLUE);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, ShouwActivity.class);
                    startActivity(intent);
                }
            });
            mSearchlinFlowLayout.addView(textView);
        }
        //点击搜索添加
        sousuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = shuru.getText().toString();
                textView=new TextView(MainActivity.this);
                textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.addedit_bg));
                textView.setPadding(5,5,5,5);
                textView.setTextSize(20);
                textView.setTextColor(Color.BLUE);
                textView.setText(s);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(MainActivity.this, ShouwActivity.class);
                        startActivity(intent);
                    }
                });
                mSearchlinFlowLayout.addView(textView);
            }
        });
    }

    private void initView() {
        sousuo = findViewById(R.id.result_search);
        shuru = findViewById(R.id.shuru);
        mSearchlinFlowLayout = findViewById(R.id.mSearchFlowLayout);
        qingkong = findViewById(R.id.qingkong);
    }
    int setChildContenView(){
        return R.layout.activity_main;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42993958/article/details/82832519