Let's take simple CustomView as an example
public class MyView extends View {
public MyView(Context context) {
super(context);
super.setBackgroundColor(Color.RED);
}
public MyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawRect(20,20,220,220,new Paint());
}
}
In my MainActivity I add my View in a LinearLayout
setContentView(R.layout.activity_main);
LinearLayout linearLayout = findViewById(R.id.linearlayout);
linearLayout.addView(new MyView(this));
linearLayout.addView(new MyView(this));
As u can see change to the background color, the first view MyView take up all the space
I want my two views to only occupy the black space. wrap_content
LinearLayout linearLayout = findViewById(R.id.linearlayout); MyView mView = new MyView(this); mView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); linearLayout.addView(mView);