Mulgard
Mulgard

Reputation: 10589

How to overlay layouts?

I programmatically create the layout of my view:

RelativeLayout.LayoutParams layoutParamsDummy = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, this.optimalCenterPadding);
layoutParamsDummy.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

RelativeLayout relativeLayoutDummy = new RelativeLayout(this.getContext());
relativeLayoutDummy.setId(DUMMY_ID);
relativeLayoutDummy.setBackground(this.getResources().getDrawable(R.drawable.custom_background));
relativeLayoutDummy.setLayoutParams(layoutParamsDummy);

relativeLayoutCenter.addView(relativeLayoutDummy);

RelativeLayout.LayoutParams layoutParamsImageViewCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParamsImageViewCenter.addRule(RelativeLayout.CENTER_HORIZONTAL);
layoutParamsImageViewCenter.setMargins(0, this.optimalCenterBackgroundPadding, 0, 0);

this.imageViewCenter = new ImageView(this.getContext());
this.imageViewCenter.setLayoutParams(layoutParamsImageViewCenter);
this.imageViewCenter.setImageDrawable(Images.loadDrawableFromFile(this.getContext(), Paths.IMAGE_BACKGROUND_POWER));
this.imageViewCenter.setBackground(null);

RelativeLayout.LayoutParams layoutParamsImageButtonCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParamsImageButtonCenter.addRule(RelativeLayout.CENTER_HORIZONTAL);
layoutParamsImageButtonCenter.addRule(RelativeLayout.ABOVE, DUMMY_ID);

this.imageButtonCenter = new ImageButton(this.getContext());
this.imageButtonCenter.setId(CENTER_ID);
this.imageButtonCenter.setLayoutParams(layoutParamsImageButtonCenter);
this.imageButtonCenter.setImageDrawable(Images.loadDrawableFromFile(this.getContext(), Paths.IMAGE_POWER));
this.imageButtonCenter.setBackground(null);
this.imageButtonCenter.setOnDragListener(new DropTargetOnDragListener());

I have to do it programmatically because i use a custom circular layout. What i have now is the following:

enter image description here

Two circles with the bigger circle below the smaller one. What i want to have is the following:

enter image description here

the bigger circle should be exactly under the smaller circle that means i want the smaller circle to overlay the bigger circle. It is a very hard task to calculate the correct position for working on every screen size and density and that leads me to the following question:

Can i place a layout exactly under another layout?

I cant just set the background of the imagebutton or something like that because the green, smaller circle is the drop area for other buttons. the bigger circle should not trigger the drop event.

I also cant work with margins because i dynamically place buttons around the green, smaller circle. I tried margins but they completly screwed up the positioning of these buttons.

Upvotes: 0

Views: 91

Answers (1)

Berťák
Berťák

Reputation: 7322

Try to use RelativeLayout.CENTER_IN_PARENT instead of RelativeLayout.CENTER_HORIZONTAL.

Also, do you add your imageViewCenter and imageButtonCenter to your relativeLayoutDummy or relativeLayoutCenter? I see a possible problem in line layoutParamsImageButtonCenter.addRule(RelativeLayout.ABOVE, DUMMY_ID); which means, that DUMMY_ID layout will be positioned (vertically) under imageButtonCenter, it will not overlay it.

Upvotes: 1

Related Questions