我以编程方式创建视图的布局:
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());
我必须以编程方式进行,因为我使用自定义的圆形布局。 我现在拥有以下内容:
两个圆圈,较小圆圈下面有一个较大的圆圈。我想要的是以下内容:
较大的圆圈应该正好在较小的圆圈下,这意味着我希望较小的圆圈覆盖较大的圆圈。计算每个屏幕尺寸和密度的正确位置是一项非常艰巨的任务,这引出了以下问题:
我可以将布局完全放在另一个布局下吗?
我无法设置图像按钮的背景或类似的东西,因为绿色的小圆圈是其他按钮的下拉区域。较大的圆圈不应该触发掉落事件。
我也无法使用边距,因为我动态地将按钮放在绿色的小圆圈周围。我尝试了边距,但他们完全搞砸了这些按钮的位置。
答案 0 :(得分:1)
尝试使用RelativeLayout.CENTER_IN_PARENT
代替RelativeLayout.CENTER_HORIZONTAL。
另外,您是否将imageViewCenter和imageButtonCenter添加到relativeLayoutDummy或relativeLayoutCenter?我看到行layoutParamsImageButtonCenter.addRule(RelativeLayout.ABOVE, DUMMY_ID);
中可能存在问题,这意味着DUMMY_ID布局将(在垂直方向上)位于imageButtonCenter下,它不会覆盖它。