<FrameLayout ...>(在布局 xml 中)将其子代绘制在另一个之上。
所以
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
< the background view />
< the informative view />
</FrameLayout>
应该可以解决问题。但请注意,移动的背景会浪费 CPU 负载和电池等资源。
UPDATE(移动:与任何其他视图一样)
要移动视图,您可以使用动画或以编程方式更改视图位置。我做了后者的回应,一触即发,你大概会选择前者。 SO上有很多about动画。
我会尝试使背景图像比屏幕宽 2 倍,将左侧坐标设置为负值,并通过动画将此值更改为 0(图像会向右移动,至少在 iOS 中是这样)。
注 1。
要在 LinearLayout 或 RelativeLayout 中定位视图,您可以使用辅助透明视图。
例如,下面的布局将屏幕分成 5 个区域:
11111111111
222 333
44444444444
并将图像放置在“2”右侧和“4”上方的角落:
<RelativeLayout
android:id="@+id/sight"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<View
android:id="@+id/sightq1"
android:layout_width="fill_parent"
android:layout_height="80px"
android:layout_alignParentTop="true"
/>
<View
android:id="@+id/sightq4"
android:layout_width="fill_parent"
android:layout_height="80px"
android:layout_alignParentBottom="true"
/>
<View
android:id="@+id/sightq2"
android:layout_width="80px"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_above="@id/sightq4"
android:layout_below="@id/sightq1"
/>
<View
android:id="@+id/sightq3"
android:layout_width="80px"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_above="@id/sightq4"
android:layout_below="@id/sightq1"
/>
<!-- example: -->
<ImageView
android:id="@+id/..."
android:layout_toRightOf="@id/sightq2"
android:layout_alignTop="@id/sightq4"
android:src="@drawable/..."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
话虽如此,我必须注意,您可能不需要辅助视图,只需定位即可。
注 2。
要获取视图的实际视图宽度/高度,您必须使用 ViewTreeObserver.OnGlobalLayoutListener 并从 调用 v.getMeasuredWidth() 和 v.getMeasuredHeight() onGlobalLayout()。这些宽度和高度只有在布局发生后才知道。
v.getViewTreeObserver().addOnGlobalLayoutListener(mLayoutListener); 注册一个监听器。