android-widget - 从 xamarin 或 monodroid 中的资源布局获取 View

标签 android-widget xamarin.android xamarin

我尝试从资源布局获取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/

相关文章:

android - 如何在安卓手机上安装所有应用程序

java - 有关小部件上下文/生命周期的信息

Android 未读消息数显示在应用程序图标上

安卓滑出菜单

xamarin.ios - 是否可以将 xamarin 部分嵌入到现有的 native 应用程序中?

Android- SetOutlineAmbientShadowColor 在 api < 28 上没有这样的方法错误

android - RecyclerView Adapter - NotifyItemRemoved - 删除的项目在滚动后再次出现

c# - 通过 Xamarin.Forms 在 Android 上打印本地文件

c# - 文字转语音不适用于不同的安卓手机

xamarin - 函数式编程和依赖倒置 : how to abstract storage?