FlowLayout and 自定义组合控件 动态赋值

//自定义组合控件的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:gravity="center">

    <Button
        android:id="@+id/btn_left_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/center_et"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Button
        android:id="@+id/btn_right_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

//在values 下创建artts

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyTitle">

        <attr name="left_tetx_btn" format="string" />
        <attr name="right_tetx_btn" format="string" />

        <attr name="center_text_ed" format="string" />
        <attr name="center_color_ed" format="color" />


    </declare-styleable>
</resources>

//自定义组合控件的类  

public class MyTitle extends LinearLayout implements View.OnClickListener {
    private static final String TAG = "MyTitle++++++++++";

    private Button leftTextBtn;
    private Button rightTextBtn;
    private TextView centerTextEd;

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

    public MyTitle(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //第二步2
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyTitle, 0, 0);
        String LeftBtn = typedArray.getString(R.styleable.MyTitle_left_tetx_btn);
        String RightBtn = typedArray.getString(R.styleable.MyTitle_right_tetx_btn);
        String TextEt = typedArray.getString(R.styleable.MyTitle_center_text_ed);
        int TextcolorEt = typedArray.getColor(R.styleable.MyTitle_center_color_ed, Color.BLACK);
        //第一步1
        View view = inflate(context, R.layout.item_mytitle, this);
        leftTextBtn = view.findViewById(R.id.btn_left_text);
        rightTextBtn = view.findViewById(R.id.btn_right_text);
        centerTextEd = view.findViewById(R.id.center_et);
        //第三步3
        leftTextBtn.setText(LeftBtn);
        rightTextBtn.setText(RightBtn);

        centerTextEd.setText(TextEt);
        centerTextEd.setBackgroundColor(TextcolorEt);


        leftTextBtn.setOnClickListener(this);
        rightTextBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_left_text:
                if (btnClickCallBack != null) {
                    btnClickCallBack.LeftBtn();
                }
                break;
            case R.id.btn_right_text:
                String abc = centerTextEd.getText().toString().trim();
                Log.d(TAG, "onClick: "+abc);
                if (btnClickCallBack != null) {
                    btnClickCallBack.RightBtn(abc);
                }
                break;
        }
    }

    public void setBtnClickCallBack(BtnClickCallBack btnClickCallBack) {
        this.btnClickCallBack = btnClickCallBack;
    }

    private BtnClickCallBack btnClickCallBack;

    public interface BtnClickCallBack {
        void LeftBtn();

        void RightBtn(String abc);
    }
}

//在activity_main中调用一下

<?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">

    <com.bwie.mytitle.MyTitle
        android:id="@+id/mytitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ebe4e4"
        app:center_color_ed="#f5f3f3"
        app:center_text_ed="123"
        app:left_tetx_btn="-"
        app:right_tetx_btn="+" />

</LinearLayout>

以上是自定义组合控件

//FlowLayout布局

public class FlowLayout extends ViewGroup {

    private View childView;

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

        int childWidth = 0;
        int childHeight = 0;
        int lineWidth = 0;
        int lineHeight = 0;
        int totalHeight = 0;
        int height = 0;
        int wight = 0;
        int leftMargin;
        int rightMargin;
        int bottomMargin;
        int topMargin;

        MarginLayoutParams layoutParams;
        for (int i = 0; i < getChildCount(); i++) {
            childView = getChildAt(i);
            childHeight = childView.getMeasuredHeight();
            childWidth = childView.getMeasuredWidth();
            layoutParams = (MarginLayoutParams) childView.getLayoutParams();
            leftMargin = layoutParams.leftMargin;
            rightMargin = layoutParams.rightMargin;
            bottomMargin = layoutParams.bottomMargin;
            topMargin = layoutParams.topMargin;
            if (childWidth > widthSize) {
                throw new IllegalArgumentException("View不能大于总宽度");
            }

            if (childWidth + lineWidth + leftMargin + rightMargin > widthSize - getPaddingLeft() - getPaddingRight()) {
                wight = widthSize;
                totalHeight += lineHeight;
                lineHeight = childHeight + topMargin + bottomMargin;
                lineWidth = childWidth + leftMargin + rightMargin;
            } else {
                lineWidth += childWidth;
                lineHeight = Math.max(lineHeight, childHeight);
                wight = Math.max(wight, lineWidth + leftMargin + rightMargin);
            }
            if (i == getChildCount() - 1) {
                totalHeight += lineHeight;
                height = totalHeight;
            }
        }

        wight = widthMode == MeasureSpec.EXACTLY ? widthSize : wight + getPaddingLeft() + getPaddingRight();
        height = heightMode == MeasureSpec.EXACTLY ? heightSize : height + getPaddingBottom() + getPaddingTop();

        setMeasuredDimension(wight, height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childWidth = 0;
        int childHeight = 0;
        int lineWidth = 0;
        int lineHeight = 0;
        int totalHeight = 0;
        int height = 0;
        int wight = 0;
        int leftMargin;
        int rightMargin;
        int bottomMargin;
        int topMargin;

        MarginLayoutParams layoutParams;
        for (int i = 0; i < getChildCount(); i++) {
            childView = getChildAt(i);
            childHeight = childView.getMeasuredHeight();
            childWidth = childView.getMeasuredWidth();
            layoutParams = (MarginLayoutParams) childView.getLayoutParams();
            leftMargin = layoutParams.leftMargin;
            rightMargin = layoutParams.rightMargin;
            bottomMargin = layoutParams.bottomMargin;
            topMargin = layoutParams.topMargin;
            if (childWidth + lineWidth + leftMargin + rightMargin > getMeasuredWidth() - getPaddingRight() - getPaddingLeft()) {

                totalHeight += lineHeight;
                lineWidth = 0;
                setLayout(childView, lineWidth + leftMargin, totalHeight, lineWidth + childWidth + rightMargin, totalHeight + childHeight + bottomMargin);
                lineHeight = childHeight + topMargin + bottomMargin;
                lineWidth = childWidth + leftMargin + rightMargin;
            } else {
                setLayout(childView, lineWidth + leftMargin, totalHeight, lineWidth + childWidth + rightMargin, totalHeight + childHeight + bottomMargin);
                lineWidth += childWidth + leftMargin + rightMargin;
                lineHeight = Math.max(lineHeight, childHeight + topMargin + bottomMargin);
            }
        }
    }

    public void setLayout(View child, int l, int t, int r, int b) {
        l += getPaddingLeft();
        t += getPaddingTop();
        r += getPaddingRight();
        b += getPaddingBottom();
        child.layout(l, t, r, b);
    }
}

在activity_main中调用一下

<?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">

    <com.bwie.mytitle.MyTitle
        android:id="@+id/mytitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ebe4e4"
        app:center_color_ed="#f5f3f3"
        app:center_text_ed="123"
        app:left_tetx_btn="-"
        app:right_tetx_btn="+" />

    <View
        android:layout_width="match_parent"
        android:layout_height="0.75dp"
        android:background="#f999" />

    <com.bwie.mytitle.FlowLayout
        android:id="@+id/flowlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="20dp" />

</LinearLayout>

//在主方法进行处理

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity++++++++++";
    private MyTitle myTitle;
    private FlowLayout flowLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myTitle = findViewById(R.id.mytitle);
        flowLayout = findViewById(R.id.flowlayout);

        myTitle.setBtnClickCallBack(new MyTitle.BtnClickCallBack() {
            @Override
            public void LeftBtn() {
                if (flowLayout.getChildCount()!=0){
                    flowLayout.removeViewAt(0);
                }
            }

            @Override
            public void RightBtn(String abc) {
                Log.d(TAG, "RightBtn: "+abc);
                TextView textView = new TextView(MainActivity.this);
                textView.setText(abc);
                textView.setTextColor(Color.RED);

                LinearLayout.LayoutParams layoutParams =
                        new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(10,10,10,10);
                flowLayout.addView(textView, layoutParams);
            }


        });
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41701790/article/details/80850914