1.实现原理
通过继承LinearLayout实现水平布局,再动态新建一个垂直布局的linearLayout,根据传入的标题名称的长度动态创建TextView和ImageView,根据顺序加到linearlayout中。
2.主类BottomView继承LinearLayout
public class BottomView extends LinearLayout { private TextView textView; private List<String> name; private List<Object> image; private List<Object> images; private Context context; private LinearLayout linearLayout; private ImageView imageView; private int color; private int size = 10; private int checkColor; private int width = 80; private int height = 80; private List<LinearLayout> linearLayouts; private List<ImageView> imageViews; private List<TextView> textViews; private boolean open = true; private boolean square = true; private Object imageContet; private int imageWidth = 200; private int imageHeight = 200; private OnClick onClick; private int imageBottom=20; public void setOnClick(BottomView.OnClick onClick) { this.onClick = onClick; } public BottomView(Context context) { super(context); this.context = context; } public BottomView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); this.context = context; } public BottomView init() { //设置垂直linearlayout的布局 LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); layoutParams.weight = 1; layoutParams.gravity = Gravity.CENTER; LayoutParams imageLayoutParams = new LayoutParams(width, height); imageLayoutParams.gravity = Gravity.CENTER; imageViews = new ArrayList<>(); linearLayouts = new ArrayList<>(); textViews = new ArrayList<>(); this.setClipChildren(false); //判断是否要打开中间按钮 if (open) { for (int i = 0; i < name.size(); i++) { final int finalI = i; //判断view的个数,是奇数即使打开了中间按钮也不起作用 if ((name.size() % 2) != 0) { textView = new TextView(context); textView.setText(name.get(i)); textView.setGravity(Gravity.CENTER_HORIZONTAL); textView.setTextColor(color); textView.setTextSize(size); textViews.add(textView); imageView = new ImageView(context); Glide.with(context).load(image.get(i)).into(imageView); imageVi