我尝试从资源布局获取MapView,但它返回null:
var map_view = FindViewById<MapView>(Resource.Id.emap);
mylayout.xml 如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/etklmap"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/zoomOutButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Zoom Out" />
<com.google.android.maps.MapView
android:id="@+id/emap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:apiKey="apikey" />
</LinearLayout>
那么有没有办法在不调用SetContentView(Resource.Layout.mylayout);的情况下获取 View
最佳答案
资源文件只是 XML - 在“膨胀”之前它不会成为 View 组和小部件的集合
膨胀 XML 的一种方法是使用 SetContentView。
另一种方法是使用 inflaterservice - 请参阅此 monodroid 问题(和答案)作为示例:Android: Getting the View added with LayoutInflator
请注意,在幕后,setcontentview 所做的只是使用完全相同的 inflaterservice 来膨胀 XML,将事件作为膨胀的父框架传入。
关于此的官方 Android 文档是 http://developer.android.com/reference/android/view/LayoutInflater.html
关于android-widget - 从 xamarin 或 monodroid 中的资源布局获取 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12482831/