LinearLayout addView 只添加一个视图

LinearLayout addView adds only one view

我试图在 LinearLayout 中添加三个水平对齐的视图,但只显示一个视图;第一个红色 circle 这里是下面的代码:

LinearLayout.LayoutParams linearParam = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        view_custom_loading = new LinearLayout(getContext());
        view_custom_loading.setVisibility(View.VISIBLE);
        view_custom_loading.setLayoutParams(linearParam);
        view_custom_loading.setBackgroundColor(getContext().getResources().getColor(R.color.semi_trans));
        view_custom_loading.setPadding(10, 10, 10, 10);
        view_custom_loading.setOrientation(LinearLayout.HORIZONTAL);

        CircleView circle = new CircleView(getContext());
        circle.color = Color.RED;
        CircleView circle1 = new CircleView(getContext());
        circle1.color = Color.WHITE;
        CircleView circle2 = new CircleView(getContext());
        circle2.color = Color.BLUE;
        view_custom_loading.addView(circle);
        view_custom_loading.addView(circle1);
        view_custom_loading.addView(circle2);
        viewList.add(circle);
        viewList.add(circle1);
        viewList.add(circle2);

这是 CircleViews onDraw 方法:

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint p = new Paint();
        p.setColor(color);
        p.setAntiAlias(true);
        p.setStyle(Paint.Style.FILL);
        canvas.drawCircle(50, 50, 25, p);
    }

您的第一个圆圈可能有 MATCH_PARENT 宽度,这就是为什么它是您看到的唯一视图。更改此参数

更新

尝试

view_custom_loading.addView(circle, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));