三色阶梯

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

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

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-"
            android:layout_weight="2"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="三色梯"
            android:textSize="24dp"
            android:layout_weight="7"/>

        <Button
            android:id="@+id/btn_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+"
            android:layout_weight="2"/>
    </LinearLayout>
<wanghuiqi.bawie.com.threecolor_jieti.LadderView
    android:id="@+id/lad_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</LinearLayout>

自定义控件LadderView

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class LadderView extends ViewGroup {
    public LadderView(Context context) {
        this(context,null);
    }

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

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

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        int startWidth = 0;
        int startHeight = 0;
        for (int i=0; i<count; i++){
            View v = getChildAt(i);
            v.layout(startWidth,startHeight,startWidth+v.getMeasuredWidth(),startHeight+v.getMeasuredHeight());
            if ((i+1)%3==0) {
                startWidth=0;
            }else {
                startWidth += v.getMeasuredWidth();
            }
            startHeight += v.getMeasuredHeight();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec,heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }
}

MainActivity

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import static java.security.AccessController.getContext;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_delete;
    private Button btn_add;
    private LadderView lad_view;
    private int i = 0;

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

    private void initView() {
        btn_delete = (Button) findViewById(R.id.btn_delete);
        btn_add = (Button) findViewById(R.id.btn_add);

        btn_delete.setOnClickListener(this);
        btn_add.setOnClickListener(this);
        lad_view = (LadderView) findViewById(R.id.lad_view);
        lad_view.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        i++;
        final TextView text = new TextView(this);
        text.setWidth(250);
        text.setHeight(100);
        text.setText(" 条目" + i);
        text.setTextColor(Color.WHITE);//字体颜色白色
        if ((i + 1) % 3 == 0) {
            text.setBackgroundColor(Color.RED);//背景颜色绿色2
        } else if ((i + 1) % 3 == 1) {
            text.setBackgroundColor(Color.GREEN);//背景颜色蓝色3
        } else if ((i + 1) % 3 == 2) {
            text.setBackgroundColor(Color.BLUE);//背景颜色红色1
        }
        lad_view.addView(text);

        //长按删除选中条目
        text.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //对话框
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("删除条目");
                builder.setMessage("请确认是否要删除选中的条目");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        lad_view.removeView(text);
                    }
                });
                builder.setNegativeButton("取消", null);
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
                return true;
            }
        });
        
        //点击删除(-)
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lad_view.removeView(text);
            }
        });
        
        //点击条目跳转页面
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "跳跳跳", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值