Android:findviewbyid:当视图不在 setContentView 调用的同一布局上时,通过 id 查找视图 [英] Android: findviewbyid: finding view by id when view is not on the same layout invoked by setContentView

查看:23
本文介绍了Android:findviewbyid:当视图不在 setContentView 调用的同一布局上时,通过 id 查找视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 MapActivity 扩展的活动 MyActivity.在包含布局的 .xml 文件中,我只能包含 MapView

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 didn't want to check old values. I just wanted to get a pointer to that view.

查看别人的代码,我刚刚找到了一种解决方法,您可以使用 LayoutInflater 访问布局的根目录.

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

代码如下,其中this是一个Activity:

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);

您需要获取this上下文的inflater,通过inflate方法访问根视图,最后在布局的根视图上调用findViewById.

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

希望这对某人有用!再见

Hope this is useful for someone! Bye

这篇关于Android:findviewbyid:当视图不在 setContentView 调用的同一布局上时,通过 id 查找视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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