幸福树或关注树

在这里插入图片描述
1.主页面布局页面

<?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"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="10dp">
        <EditText
            android:id="@+id/seach_ed"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            />
        <ImageView
            android:id="@+id/seach_image"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/search"
            />
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="搜索历史"
        android:textColor="#00ff00"
        />
    <com.example.mytree.CustomTreeView
        android:id="@+id/customtree"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        />
</LinearLayout>

2.自定义view页面

package com.example.mytree;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

public class CustomTreeView extends LinearLayout {

    private Paint paint;

    public CustomTreeView(Context context) {
        super(context);
        init();
    }

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

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

    private void init() {
        setWillNotDraw(false);
        paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(3);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        measureChildren(widthMeasureSpec,heightMeasureSpec);
        int childCount = getChildCount();
        int bottom = 0;
        for(int i = 0;i<childCount;i++){
            View view = getChildAt(i);
            bottom += view.getMeasuredHeight();
        }
        setMeasuredDimension(sizeWidth,bottom);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        int left = 0,top = 0;
        int childCount = getChildCount();
        for(int i = 0;i<childCount;i++){
            View view = getChildAt(i);
            if(i % 2==0){
                left = 0;
            }else{
                left = getMeasuredWidth()/2;
            }
            view.layout(left,top,left+getMeasuredWidth()/2,top + view.getMeasuredHeight());
            top += view.getMeasuredHeight();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLine(getMeasuredWidth()/2,0,getMeasuredWidth()/2,getMeasuredHeight(),paint);
    }
}

3.Mainactivity页面

package com.example.mytree;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private EditText editText;
    private CustomTreeView customTreeView;

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

    private void initView() {
        imageView = findViewById(R.id.seach_image);
        editText = findViewById(R.id.seach_ed);
        customTreeView = findViewById(R.id.customtree);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textView = new TextView(MainActivity.this);
                textView.setTextColor(Color.RED);
                textView.setText(editText.getText().toString().trim());
                textView.setBackgroundResource(R.drawable.text_shape);
                customTreeView.addView(textView);
                customTreeView.invalidate();
            }
        });
    }
}

连接如下:
https://github.com/guoxinyu1995/yuekao/tree/master/mytree

猜你喜欢

转载自blog.csdn.net/guoxinyu1207/article/details/85136456
今日推荐