我希望我的图像按钮保持它们的纵横比,所以我关注了https://stackoverflow.com/a/7966838/135135
并将背景设置为空,然后将图像按钮源图像设置为图像按钮。现在的问题是图像按钮不再像以前那样与父级对齐。如何使它再次与父右边缘对齐?
ImageButton bt = new ImageButton(this);
bt.setImageResource(R.drawable.next_button);
bt.setBackgroundDrawable(null);
bt.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
bt.setLayoutParams(params);
return bt;