我有一个FrameLayout,它有2个图像,一个填充FrameLayout的大图像和一个我想要移动的很小的图像。ImageView setMargins不起作用
我尝试移动小一个这样的: xml文件
<FrameLayout android:id="@+id/layTrackMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
<ImageView android:id="@+id/imgTrackMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView android:id="@+id/imgPosition"
android:layout_width="wrap_content"
android:src="@drawable/position"
android:layout_height="wrap_content"
/>
</FrameLayout>
,代码:
imgPosition = (ImageView)findViewById(R.id.imgPosition);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
//Neither this:
//lp.setMargins(30, 20, 0, 0);
//Or this
lp.leftMargin=30;
lp.topMargin=80;
imgPosition.setLayoutParams(lp);
小画面不动。我希望能够在布局中移动小图像。
后编辑: 在尝试了几个建议后,我得出结论,更简单的做一个自定义视图并重写onDraw来完成这项工作。
这在理论上是有效的。但是,如果您想添加背景可绘制(例如,指示您的图像被选中),这将拉伸它。更好的解决方案是使用边距定义LayoutParam对象,并将Fede的重力设置为Fede。或者,如果您想要走这条路线,请进行自定义视图。 – Dan 2011-05-29 12:24:45