package com.example.jingdong.FirstPages.FirstPageSearchs; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import com.example.jingdong.Application.ImageLoaderApp; /** * Created by Administrator on 2017/12/8 0008. */ public class HotSearch extends ViewGroup{ private HotInterface hotInterface; public void getString(HotInterface hotInterface){ this.hotInterface = hotInterface; } public HotSearch(Context context) { super(context); } public HotSearch(Context context, AttributeSet attrs) { super(context, attrs); } public HotSearch(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 i, int i1, int i2, int i3) { /** * 一般是定义为int top;一个top实际上是数组的下标 left : 指定矩形框左上角的x坐标 top: 指定矩形框左上角的y坐标 right: 指定矩形框右下角的x坐标 bottom:指定矩形框右下角的y坐标 */ int width = getWidth(); int height = getHeight(); int tw = 0; int th = 0; for (int t = 0; t < getChildCount(); t++) { final View child = getChildAt(t); if (tw + child.getWidth() < width) { } else { tw = 0; th += child.getMeasuredHeight(); //超过屏幕的宽度,自动换行 } child.layout(tw, th, tw + child.getMeasuredWidth(), th + child.getMeasuredHeight()); tw += child.getMeasuredWidth(); child.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Button bt = (Button) child; String s = bt.getText().toString(); int r = 0; if(ImageLoaderApp.oldarr!=null){ for(int i=0;i<ImageLoaderApp.oldarr.size();i++){ if(ImageLoaderApp.oldarr.get(i).equals(s)){ r = 1; } } } if(r==0){ ImageLoaderApp.oldarr.add(s); } hotInterface.getText(s); } }); } } }
简单自定义View流式布局实现模版
最新推荐文章于 2025-08-02 08:36:31 发布
