自定义View双梯形布局+自定义标题栏(点击事件)

//自定义标题栏

public class MyTitleView extends LinearLayout implements View.OnClickListener{
    public MyTitleView(Context context) {
        super(context);
    }

    public MyTitleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTitleView);
        String text = typedArray.getString(R.styleable.MyTitleView_title_text);
        String left = typedArray.getString(R.styleable.MyTitleView_title_left_btn);
        String right = typedArray.getString(R.styleable.MyTitleView_title_right_btn);
        View inflate = inflate(context, R.layout.main_item, this);
        TextView title = inflate.findViewById(R.id.tv_title);
        Button btn1=inflate.findViewById(R.id.left_btn);
        Button btn2=inflate.findViewById(R.id.right_btn);
        title.setText(text);
        title.setTextColor(Color.BLACK);
        btn1.setText(left);
        btn2.setText(right);
        title.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_btn:
                if (onBtnOnclick!=null){
                    onBtnOnclick.getLeftBtn();
                }
                break;
            case R.id.right_btn:
                if (onBtnOnclick!=null){
                    onBtnOnclick.getRightBtn();
                }
                break;
            case R.id.tv_title:
                if (onBtnOnclick!=null){
                    onBtnOnclick.getText();
                }
                break;

        }
    }
    public interface onBtnOnclick{
        void getLeftBtn();
        void getRightBtn();
        void getText();
    }
    private onBtnOnclick onBtnOnclick;
    public void setOnBtnOnclick(onBtnOnclick onBtnOnclick){
        this.onBtnOnclick=onBtnOnclick;
    }
}




//梯形布局

public class FlowLayout extends ViewGroup{

    private int widthSize;

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

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int width = 0;
        int height = 0;
        int lineWidth = 0;
        int lineHeight = 0;
        int totalHeight = 0;
        View childView;
        int childWidth = 0;
        int childHeight = 0;

        for (int i = 0; i < getChildCount(); i++) {
            childView = getChildAt(i);
            childWidth = childView.getMeasuredWidth();
            childHeight = childView.getMeasuredHeight();
            if (lineWidth+childWidth>= widthSize) {
                width = widthSize;
                totalHeight += lineHeight;
                lineHeight = childHeight;
                lineWidth = childWidth;

            } else {
                lineWidth += childWidth;
                lineHeight = Math.max(lineHeight, childHeight);
                width = Math.max(width, lineWidth);
            }
            if (i == getChildCount() - 1) {
                totalHeight += lineHeight;
                height = totalHeight;
            }

        }

        width = widthMode == MeasureSpec.EXACTLY ? widthSize : width;
        height = heightMode == MeasureSpec.EXACTLY ? heightSize : height;
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int lineWidth = 0;
        int lineHeight = 0;
        int totalHeight = 0;
        View childView;
        int childWidth = 0;
        int childHeight = 0;

        for (int i = 0; i < getChildCount(); i++) {
            childView = getChildAt(i);
            childWidth = widthSize/2;
            childHeight = childView.getMeasuredHeight();
            if (i%2==0) {
                totalHeight += childHeight;
                lineWidth = 0;
                layoutChildView(childView, lineWidth, totalHeight, lineWidth + childWidth, totalHeight + childHeight);
            } else {
                totalHeight += childHeight;
                lineWidth = childWidth;
                layoutChildView(childView, lineWidth, totalHeight, lineWidth + childWidth, totalHeight + childHeight);
            }
        }

    }

    public void layoutChildView(View child, int l, int t, int r, int b) {
        child.layout(l, t, r, b);
    }
}




//MainActivity

public class MainActivity extends AppCompatActivity {

    private MyTitleView myTitleView;
    private FlowLayout flowLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myTitleView = findViewById(R.id.mytitleview);
        flowLayout = findViewById(R.id.flowlayout);
        myTitleView.setOnBtnOnclick(new MyTitleView.onBtnOnclick() {
            @Override
            public void getLeftBtn() {
                boolean b;
                if (flowLayout.getChildAt(0)!=null) {
                    b=true;
                }else{
                    b=false;
                    Toast.makeText(MainActivity.this,"数据已清空",Toast.LENGTH_SHORT).show();
                }
                if (b){
                    flowLayout.removeViewAt(0);
                }
            }

            @Override
            public void getRightBtn() {
                Toast.makeText(MainActivity.this,"加一",Toast.LENGTH_SHORT).show();
                TextView textView= new TextView(MainActivity.this);
                textView.setText("加一个");
                textView.setTextSize(26);
                textView.setBackgroundColor(Color.RED);
                textView.setWidth(flowLayout.getWidth()/2);
                textView.setHeight(60);
                flowLayout.addView(textView);
            }

            @Override
            public void getText() {
                Toast.makeText(MainActivity.this,"清空",Toast.LENGTH_SHORT).show();
                flowLayout.removeAllViews();
            }
        });
    }
}




//布局

<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"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <com.example.admin.moni1.MyTitleView
        android:id="@+id/mytitleview"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:title_text="清空"
        app:title_left_btn="-"
        app:title_right_btn="+"></com.example.admin.moni1.MyTitleView>
    <com.example.admin.moni1.FlowLayout
        android:id="@+id/flowlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:background="@color/colorPrimary"
        android:text="1"
        android:gravity="center"
        android:textSize="26dp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:background="#363"
            android:text="2"
            android:gravity="center"
            android:textSize="26dp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:background="#9f9"
            android:text="3"
            android:gravity="center"
            android:textSize="26dp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:background="#f66"
            android:text="4"
            android:gravity="center"
            android:textSize="26dp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:background="#0f0"
            android:text="5"
            android:gravity="center"
            android:textSize="26dp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:background="#f00"
            android:text="6"
            android:gravity="center"
            android:textSize="26dp"/>
    </com.example.admin.moni1.FlowLayout>

</LinearLayout>

//子布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <Button
        android:id="@+id/left_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"/>
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="6"
        android:gravity="center"/>
    <Button
        android:id="@+id/right_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2" />
</LinearLayout>

//自定义属性

<resources>
    <declare-styleable name="MyTitleView">
        <attr name="title_text" format="string"/>
        <attr name="title_left_btn" format="string"/>
        <attr name="title_right_btn" format="string"/>
    </declare-styleable>
</resources>


猜你喜欢

转载自blog.csdn.net/aa15362415/article/details/80643546