兴趣树

本文介绍了一个自定义的树形视图组件的实现,该组件使用了LinearLayout作为基类,并通过重写onMeasure、onLayout和onDraw方法来绘制树状结构。组件能够动态添加子节点并调整布局,同时支持点击事件来添加新的节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Activity页面

public class TreeActivity extends AppCompatActivity {

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

    initView();
}

private void initView() {
    final CustomTreeView customTreeView = findViewById(R.id.cfl_search);
    final EditText editText = findViewById(R.id.edit_search);
    findViewById(R.id.iv_search).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView tv = new TextView(TreeActivity.this);
            tv.setTextColor(Color.RED);
            tv.setText(editText.getText());
            tv.setBackgroundResource(R.drawable.search_history_bg);
            customTreeView.addView(tv);
            customTreeView.invalidate();
        }
    });
}
}

2.自定义view

public class CustomTreeView extends LinearLayout {
private Paint mPaint;

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

private void init(){
    setWillNotDraw(false);
    mPaint = new Paint();
    mPaint.setColor(Color.BLACK);
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.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(), mPaint);
}
}

3.xml布局

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="10dp">

    <EditText
        android:id="@+id/edit_search"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/iv_search"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginLeft="10dp"
        android:background="@mipmap/search" />
</LinearLayout>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="搜索历史"
    android:textColor="#00ff00" />

<com.bawei.myshopcar.tree.CustomTreeView
    android:id="@+id/cfl_search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp" />


</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值