通常你会有这个:
Activity parent = (Activity) context;
dragImage = (ImageView) parent.findViewById(R.id.what_ever_you_defined_on_xml);
但是,在我的情况下,我希望“findViewById”动态地获取 id 属性,因为我有多个属性,具体取决于用户在地图上想要的 OverlayItem 类型(打开街道地图)。我已经尝试过了,但是我的应用程序崩溃了:
Activity parent = (Activity) context;
View v = parent.getCurrentFocus();
dragImage = (ImageView) parent.findViewById(v.getId());
有什么建议吗?谢谢。
添加上下文的更多细节:
public Itemizedoverlay(Drawable defaultMarker, Context context, MapView map) {
super(defaultMarker, new DefaultResourceProxyImpl(context));
this.defaultMarker = defaultMarker;
this.map = map;
// Setup for dragging:
// Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle):
Activity parent = (Activity) context;
View v = parent.getCurrentFocus();
dragImage = (ImageView) parent.findViewById(v.getId());
xDragImageOffset = dragImage.getDrawable().getIntrinsicWidth() / 2;
yDragImageOffset = dragImage.getDrawable().getIntrinsicHeight();
}
在 XML 中:
<ImageView
android:id="@+id/drag1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/symbol1"
android:visibility="gone"
/>
<ImageView
android:id="@+id/drag2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/symbol2"
android:visibility="gone" />
<ImageView
android:id="@+id/drag3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/symbol3"
android:visibility="gone"
/>
<ImageView
android:id="@+id/drag4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/symbol4"
android:visibility="gone"
/>