安卓:findviewbyid:发现鉴于ID时的观点是不通过的setContentView调用相同的布局 [英] Android: findviewbyid: finding view by id when view is not on the same layout invoked by setContentView

查看:173
本文介绍了安卓:findviewbyid:发现鉴于ID时的观点是不通过的setContentView调用相同的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动MyActivity从MapActivity延伸。在包含布局我只能包括的MapView .xml文件

I have an activity MyActivity that extends from MapActivity. In the .xml file containing the layout I can only include the MapView

<com.google.android.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/trail_map_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:apiKey="key"
/>

不过,我需要找到另一种观点,即位于另一个.xml文件。 不幸的是,findViewById返回null。 我怎样才能得到我想要的看法?

However I do need to find another view that is located in another .xml file. Unfortunately, findViewById returns null. How can I get the view I am looking for?

非常感谢!

推荐答案

感谢您的评论,我明白你的意思,但我没有想检查旧值。我只是想获得一个指向这一观点。

Thanks for commenting, I understand what you mean but I didnt want to check old values. I just wanted to get a pointer to that view.

看着大人物code我刚刚发现了一个解决方法,您可以使用 LayoutInflater 访问布局的根。

Looking at somebodies code I have just found a workaround, you can access the root of a layout using LayoutInflater.

在code是以下内容,其中是一种活动:

The code is the following, where this is an Activity:

final LayoutInflater factory = getLayoutInflater();

final View textEntryView = factory.inflate(R.layout.landmark_new_dialog, null);

landmarkEditNameView = (EditText) textEntryView.findViewById(R.id.landmark_name_dialog_edit);

基本上得到充气的的背景下,通过充气方法来访问根视图,最后调用 findViewById 的布局的根视图。

Basically get the inflater for this context, access the root view through inflate method and finally call findViewById on the root view of the layout.

希望是有用的人!再见

这篇关于安卓:findviewbyid:发现鉴于ID时的观点是不通过的setContentView调用相同的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆