自定义View+流式布局

//MianActivity类
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private MyView myView;
    private Button btn_add;//点击添加条目
    private Button btn_delete;
    int i=0;
    private TextView tv;
    private Button dddd;
    private static final String TAG = "MainActivity";
    // private List<View> list = new ArrayList<>();
    int j=0;

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

        btn_add = findViewById(R.id.btn_add);//找控件
        btn_add.setOnClickListener(this);//添加点击事件
        dddd = findViewById(R.id.dddd);
        btn_delete = findViewById(R.id.btn_delete);
        dddd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myView.removeAllViews();
            }
        });

   }

    @Override
    public void onClick(View view) {
        i++;
        final TextView text= new TextView(this);
        text.setWidth(250);
        text.setHeight(100);
        text.setText(" 这是添加的条目 "+i);
        text.setTextColor(Color.WHITE);//字体颜色白色
        if((i+1)%2==0){
            text.setBackgroundColor(Color.GREEN);//背景颜色绿色2
        }else if ((i+1)%2==1){
            text.setBackgroundColor(Color.BLUE);//背景颜色蓝色3
        }/*else if ((i+1)%3==2){
            text.setBackgroundColor(Color.RED);//背景颜色红色1
        }*/
        myView.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) {
                        myView.removeView(text);
                    }
                });
                builder.setNegativeButton("取消",null);
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
                return true;
            }
        });

        /**
         * 左上角点击删除
         */

        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                j++;
                Log.d(TAG, "onClick: "+j);
             myView.removeView(text);
               /* for (int i=0;i<list.size();i++) {
                    if (i==list.size()-1) {

                        myView.removeView(list.get(i));

                        list.remove(text);

                    }
                }*/
            }

        });

        /**
         * 点击条目跳转新的页面
         */
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);
            }
        });
    }
}




//Myview类

public class MyView extends ViewGroup {
    public MyView(Context context) {
        super(context);
    }

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

    public MyView(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);
    }

    @Override
    protected void onLayout(boolean b, int l, int i1, int i2, int i3) {
        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)%2==0) {
                startWidth=0;
            }else {
                startWidth += v.getMeasuredWidth();
            }
            startHeight += v.getMeasuredHeight();
        }
    }
}



//布局文件
<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:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btn_delete"
            android:layout_weight="2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-"/>
        <Button
            android:layout_weight="6"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="24dp"
            android:id="@+id/dddd"
            android:gravity="center"
            android:text="ab"
            />
        <Button
            android:id="@+id/btn_add"
            android:layout_weight="2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+"/>
    </LinearLayout>

    <com.example.myapplication6_9zhoukaolianxi.MyView
        android:id="@+id/myView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </com.example.myapplication6_9zhoukaolianxi.MyView>

</LinearLayout>
 //有一个问题不能循环删除 只能删除最后一个条目 

猜你喜欢

转载自blog.csdn.net/wwe11122/article/details/80632241