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

前端 未结 5 2043
陌清茗
陌清茗 2020-12-08 04:02

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



        
相关标签:
5条回答
  • 2020-12-08 04:23

    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.

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

    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

    0 讨论(0)
  • 2020-12-08 04:24

    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) {
    
            }
    
        }
    
    0 讨论(0)
  • 2020-12-08 04:29

    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);
    
    0 讨论(0)
  • 2020-12-08 04:40

    try:

    Activity parentActivity = this.getParent();
    if (parentActivity != null)
    {
        View landmarkEditNameView = (EditText) parentActivity.findViewById(R.id. landmark_name_dialog_edit);
    }
    
    0 讨论(0)
  • 2020-12-08 04:50

    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.

    0 讨论(0)
提交回复
热议问题