我有一個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