【问题标题】:Android: findviewbyid: finding view by id when view is not on the same layout invoked by setContentViewAndroid:findviewbyid:当视图不在 setContentView 调用的同一布局上时,按 id 查找视图
【发布时间】:2010-02-16 08:52:44
【问题描述】:

我有一个从 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"
/>

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

非常感谢!

【问题讨论】:

  • 你为什么需要那个?你想做什么?
  • 显示不存在的视图是一种常见情况。例如,如果您希望在用户执行操作(例如按下按钮、单击菜单图标等)时显示视图。

标签: android layout


【解决方案1】:

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

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

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

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

希望这对某人有用!再见

【讨论】:

    【解决方案2】:

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

    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) {
    
            }
    
        }
    

    【讨论】:

      【解决方案3】:

      尝试:

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

      【讨论】:

        【解决方案4】:

        另一种方法是:

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

          • 不可能什么都不是。
          【解决方案6】:

          我用过

          View.inflate(getContext(), R.layout.whatever, null)
          

          View.inflate 的使用可防止在getLayoutInflater().inflate() 处使用null 的警告。

          【讨论】:

            【解决方案7】:

            在主 Activity 中只需创建如下所示的静态变量。

                //Create Static variable
                public static View mainView;
            
                 LayoutInflater inflater = getLayoutInflater();
                            View view = inflater.inflate(R.layout.activity_home, null);
                
                 mainView = view;
               //Now just need to Call MainActivity.mainView to Access your home view or Something else.
             
            

            【讨论】:

              猜你喜欢
              • 2023-03-27
              • 1970-01-01
              • 2011-06-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多