MainActivity
public class MainActivity extends AppCompatActivity {
private CustomTreeView cfl_search;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cfl_search=findViewById(R.id.cfl_search);
editText=findViewById(R.id.edit_search);
init();
}
private void init() {
findViewById(R.id.iv_search).set Listener(new View. Listener() {
@Override
public void (View v) {
TextView view = new TextView(MainActivity.this);
view.setText(editText.getText().toString());
cfl_search.addView(view);
cfl_search.invalidate();
}
});
}
CustomView
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 childAt = getChildAt(i);
if(i%2==0){
left=0;
}else{
left=getMeasuredWidth()/2;
}
childAt.layout(left,top,left+getMeasuredWidth(),top+getMeasuredHeight());
top+=childAt.getMeasuredHeight();
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(getMeasuredWidth()/2,0,getMeasuredWidth()/2,getMeasuredHeight(),paint);
}
的布局
<LinearLayout ns:android=\"http://schemas.android.com/apk/res/android\"
ns: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\" />
<Button
android:id=\"@+id/iv_search\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:layout_marginLeft=\"10dp\"
android:text=\"计入\"/>
</LinearLayout>
<TextView
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:text=\"搜索历史\"
android:textColor=\"#00ff00\" />
<com.bwei.xingqushu.CustomTreeView
android:id=\"@+id/cfl_search\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:layout_marginTop=\"10dp\" />
</LinearLayout>
继续阅读与本文标签相同的文章
-
springboot数据库主从方案
2026-05-18栏目: 教程
-
如何查找前置补丁-Windows安装补丁提示需要安装前置补丁
2026-05-18栏目: 教程
-
月活用户达7.55亿,阿里淘系如何在后流量时代引爆用户增长? | 9月17号栖夜读
2026-05-18栏目: 教程
-
B树和B+树索引原理
2026-05-18栏目: 教程
-
汇编(八)[bx] 、Loop指令
2026-05-18栏目: 教程
