Android 流式布局 + 自定义组合控件

自定义组合控件

package yanjupeng.bawei.com.day09.two;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import yanjupeng.bawei.com.day09.R;

public class TwoLayout extends LinearLayout {

    public TextView textView;

    public EditText editText;

    public TwoLiu twoLiu;

    public LinearLayout linearLayout;


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

    public TwoLayout(final Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.twolayout , this);
        textView = view.findViewById(R.id.two_text_id);
        editText = view.findViewById(R.id.two_ed_id);
        twoLiu = view.findViewById(R.id.two_liu_id);
        linearLayout = view.findViewById(R.id.two_layout_id);
        textView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String string = editText.getText().toString();
                Log.e("tag" , "textView ====="+string );
                final TextView textView = new TextView(context);
                textView.setTag(string);
                textView.setText(string);
                textView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.e("tag" , "textView =====" + textView.getTag().toString());
                    }
                });
                twoLiu.addView(textView);
                linearLayout.requestLayout();


            }
        });


    }

    public TwoLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:id="@+id/two_layout_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <EditText
                android:id="@+id/two_ed_id"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                />

            <TextView
                android:id="@+id/two_text_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="搜索"
                android:textSize="20sp"
                android:background="#4ff"
                />
        </LinearLayout>


        <yanjupeng.bawei.com.day09.two.TwoLiu
            android:id="@+id/two_liu_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#ccc"></yanjupeng.bawei.com.day09.two.TwoLiu>



    </LinearLayout>



</android.support.constraint.ConstraintLayout>

activity

package yanjupeng.bawei.com.day09.two;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import yanjupeng.bawei.com.day09.R;

public class TwoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".two.TwoActivity">

    <yanjupeng.bawei.com.day09.two.TwoLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"></yanjupeng.bawei.com.day09.two.TwoLayout>


</android.support.constraint.ConstraintLayout>

流式布局

package yanjupeng.bawei.com.day09.two;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;

public class TwoLiu extends ViewGroup {

    public int mLeftMargin = 20;
    public int mTopMargin = 20;

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

    public TwoLiu(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.e("tag" , "onMeasure");

        measureChildren(widthMeasureSpec ,heightMeasureSpec);
        int leftMargin = mLeftMargin;
        int topMargin = mTopMargin;



        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modelWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeHeigth = MeasureSpec.getSize(heightMeasureSpec);
        int modelHeigth = MeasureSpec.getMode(heightMeasureSpec);

        switch (modelHeigth){
            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 > getMeasuredWidth()){
                        leftMargin = mLeftMargin;
                        topMargin += measuredHeight + mTopMargin;
                    }
                    leftMargin += measuredWidth + mLeftMargin;
                }
                topMargin += measuredHeight + mTopMargin;

                break;

        }
        setMeasuredDimension(sizeWidth , topMargin);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        Log.e("tag" , "onLayout");
        int leftMargin = mLeftMargin;
        int topmargin = mTopMargin;

        for (int j = 0 ; j < getChildCount() ; j ++){
            int measuredHeight = getChildAt(j).getMeasuredHeight();
            int measuredWidth = getChildAt(j).getMeasuredWidth();
            if (leftMargin + measuredWidth + mLeftMargin > getWidth()){
                leftMargin = mLeftMargin;
                topmargin += measuredHeight + mTopMargin;

                getChildAt(j).layout(leftMargin , topmargin , measuredWidth + leftMargin , measuredHeight + topmargin  );
            }else {
                getChildAt(j).layout(leftMargin , topmargin , measuredWidth + leftMargin ,measuredHeight + topmargin  );
            }
            leftMargin += measuredWidth + mLeftMargin;
        }

    }


}

猜你喜欢

转载自blog.csdn.net/qq_42886907/article/details/89064858