【发布时间】:2011-11-10 19:51:19
【问题描述】:
通常你会这样:
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"
/>
【问题讨论】:
标签: android xml view itemizedoverlay