我有一个ImageView,当我尝试以编程方式设置它的边距时,它的src是一个动画列表,它也非常小地调整了imageview的大小。
ImageView hand = (ImageView)rlRoot.findViewById(R.id.ivHandAnimation);
MarginLayoutParams marginsParams = new MarginLayoutParams(hand.getLayoutParams());
marginsParams.setMargins(this.getLeft(), rlRoot.getMeasuredHeight() - 20, 0, 0);
RelativeLayout.LayoutParams lp = new LayoutParams(marginsParams);
//lp.height = rlRoot.findViewById(R.id.btnHole_14).getMeasuredHeight();
hand.setLayoutParams(lp);
if (hand != null) {
hand.setVisibility(View.VISIBLE);
AnimationDrawable frameAnimation = (AnimationDrawable)hand.getDrawable();
frameAnimation.setCallback(hand);
frameAnimation.setVisible(true, true);
}
我试过设置lp.height但它不起作用。我还使用以下代码创建LayoutParams。
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
谢谢。
答案 0 :(得分:0)
问题出在rlRoot.getMeasuredHeight() - 20
。 onCreate方法中的rlRoot.getMeasuredHeight()
会返回0.所以rlRoot.getMeasuredHeight()-20表示你将高度减少了20个像素。
答案 1 :(得分:0)
我忘了回答这个问题。在我的情况下,当ImageView调整大小时,当我使用hand.setLayoutParams(lp);
属性设置边距android:adjustViewBounds
时,它保持其第一个宽度和高度为我解决问题。