我试图在LinearLayout中添加三个水平对齐的视图,但只显示一个视图;这里的第一个RED 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);
}
答案 0 :(得分:4)
可能你的第一个圆圈有MATCH_PARENT宽度,这就是为什么它是你看到的唯一视图。更改此参数
<强>更新强>
尝试
view_custom_loading.addView(circle, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));