Android:findviewbyid:当视图不在 setContentView 调用的同一布局上时,通过 id 查找视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2271570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:14:19  来源:igfitidea点击:

Android: findviewbyid: finding view by id when view is not on the same layout invoked by setContentView

androidlayout

提问by Santiago

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

我有一个从 MapActivity 扩展的活动 MyActivity。在包含布局的 .xml 文件中,我只能包含 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"
/>

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?

但是我确实需要找到位于另一个 .xml 文件中的另一个视图。不幸的是, findViewById 返回 null。如何获得我正在寻找的视图?

Thanks a lot!

非常感谢!

回答by Santiago

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.

感谢您的评论,我明白您的意思,但我不想检查旧值。我只是想得到一个指向那个视图的指针。

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

查看其他人的代码,我刚刚找到了一种解决方法,您可以使用LayoutInflater.

The code is the following, where thisis an Activity:

代码如下,其中this是一个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);

You need to get the inflater for thiscontext, access the root view through the inflate method and finally call findViewByIdon the root view of the layout.

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

Hope this is useful for someone! Bye

希望这对某人有用!再见

回答by user5076776

I have changed in my activity but effected. Here is my code:

我的活动发生了变化,但有效。这是我的代码:

View layout = getLayoutInflater().inflate(R.layout.list_group,null);
        try {
            LinearLayout linearLayout = (LinearLayout) layout.findViewById(R.id.ldrawernav);
            linearLayout.setBackgroundColor(Color.parseColor("#ffffff"));
        }
        catch (Exception e) {

        }

    }

回答by richy

try:

尝试:

Activity parentActivity = this.getParent();
if (parentActivity != null)
{
    View landmarkEditNameView = (EditText) parentActivity.findViewById(R.id. landmark_name_dialog_edit);
}

回答by JFreeman

Another way to do this is:

另一种方法是:

// inflate the layout
View myLayout = LayoutInflater.from(this).inflate(R.layout.MY_LAYOUT,null);

// load the text view
TextView myView = (TextView) myLayout.findViewById(R.id.MY_VIEW);

回答by skyman

It's impossible. You can only find and access views that are currently running. If you want to check the value of ex. TextView used in previus activity you must save the value is SharedPreferences, database, file or pass by Intent.

不可能。您只能查找和访问当前正在运行的视图。如果要检查 ex 的值。在以前的活动中使用的 TextView 必须保存的值为 SharedPreferences、数据库、文件或通过 Intent 传递。