简体   繁体   English

Android-从膨胀的布局中查找视图的ID

[英]Android - Finding ID of view from inflated layout

I know there are lots of other answers on stackoverflow on the same thing but I can't seem to get it to work. 我知道关于同一件事在stackoverflow上还有很多其他答案,但是我似乎无法使其正常工作。

What I'm trying to do is find the ID of a view from an inflated layout. 我想做的是从膨胀的布局中找到视图的ID。 I want WV1 to load google.com when the button is clicked, you can see I'm using onClick from XML to do this. 我希望WV1在单击按钮时加载google.com,您可以看到我正在使用XML的onClick来执行此操作。

public void ButtonClicked(View view)
        { 

        View inflatedView = getLayoutInflater().inflate(R.layout.tab_content, null);
        WV1 = (WebView) inflatedView.findViewById(R.id.tab1WV);
        WV1.setWebViewClient(new InsideWebViewClient());

          if (WV1.isShown()) {
                WV1.requestFocus();
                    }
                else{
                }

          if (WV1.isFocused()) {
                WV1.loadUrl("http://www.google.com"); 
                }
        }

This is in the MainActivity, the webview (WV1) is in the other, inflated class. 这在MainActivity中,Webview(WV1)在另一个膨胀类中。

Problem is, nothing happens at all... 问题是,什么都没有发生...

I've been stuck on this for quite some time now, I appreciate all help given to me.. If there's any other information you require then just ask, thanks in advance! 我已经为此停留了很长时间,感谢您给予我的所有帮助。如果您需要任何其他信息,请先提出,谢谢!

--Edit-- In the MainActivity, theres tabhost and a button that creates new tabs. -编辑-在MainActivity中,有一个tabhost和一个用于创建新标签的按钮。 When new tabs are created the MainActivity inflates the second class file containing the webview, I can't get the mainactivity to find the webview from the inflated class. 创建新选项卡时,MainActivity会膨胀包含Webview的第二个类文件,但我无法通过mainActivity从膨胀的类中查找Webview。 I dont know if this helps any more or not... 我不知道这是否有帮助...

You're not attaching it to anything. 您没有将它附加到任何东西上。 You need to either supply the parent when you inflate it, or call addView on the ViewGroup that should contain it. 您需要在为父级充气时提供其父级,或者在应包含该父级的ViewGroup上调用addView。

public void ButtonClicked(View view){ 

    View inflatedView = getLayoutInflater().inflate(R.layout.tab_content, (ViewGroup) findViewById(android.R.id.content));
    WV1 = (WebView) inflatedView.findViewById(R.id.tab1WV);
    WV1.setWebViewClient(new InsideWebViewClient());

    if (WV1.isShown()) {
        WV1.requestFocus();
    }

     if (WV1.isFocused()) {
         WV1.loadUrl("http://www.google.com"); 
    }
}

Or: 要么:

((ViewGroup)findViewById(android.R.id.content)).addChild(WV1);

Both of these will add your view at the end. 这两个都将在最后添加您的视图。 You may need to add some layout attributes to get it to look like you want. 您可能需要添加一些布局属性,以使其看起来像您想要的。

Check out this link: 查看此链接:

Android - Add textview to layout when button is pressed Android-按下按钮时将textview添加到布局

Where one of the answers does this: mLayout.addView(createNewTextView(mEditText.getText().toString())); 答案之一就是这样做的:mLayout.addView(createNewTextView(mEditText.getText()。toString()));

where mLayout is a linear layout in the activity. 其中mLayout是活动中的线性布局。

You'd have to add a view to one of your current layouts or start up an activity that opens up with a web view in it. 您必须将视图添加到当前布局之一,或者启动一个在其中打开Web视图的活动。

Alternative way to create webview : 创建webview的另一种方法:

Webview WV1 = new WebView(view.getContext);
WV1.setWebViewClient(new InsideWebViewClient());

  if (WV1.isShown()) {
        WV1.requestFocus();
            }
        else{
        }

  if (WV1.isFocused()) {
        WV1.loadUrl("http://www.google.com"); 
        }
}

Now add this to a view group 现在将其添加到视图组

viewgroup.addchild(WV1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM