0

Following is a xml: welcome_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white" >

    <ImageView
        android:id="@+id/welcome_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.demo.src.WelcomeLayout
        android:id="@+id/welcome_ad"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />    
</RelativeLayout>

This is the MainActivity, I setContentView with layout welcome_view.xml.

class MainActivity extends Activity
{
    onCreate()
    {
        setContentView(R.layout.welcome_view);
        ViewGroup view1 = findViewByID(R.id.ad_view);
    }
}

WelcomeLayout has been contained in the welcome_view.xml. Please tell me the view in the following class is different with the one in above class??? Why, tell me some the inner mechanism.

class WelcomeLayout extends FrameLayout
{

  onCreate()
  {
      super(context);
      View.inflate(context, R.layout.welcome_view, null);
      ViewGroup view2 = findViewById(R.id.ad_view);
   }
}

Thanks!

Ruiyi Luo
  • 9
  • 2

1 Answers1

0

reference:Difference between setContentView and LayoutInflater

setContentView is an Activity method only. Each Activity is provided with a FrameLayout with id "@+id/content" (i.e. the content view). Whatever view you specify in setContentView will be the view for that Activity. Note that you can also pass an instance of a view to this method, e.g. setContentView(new WebView(this)); The version of the method that you are using will inflate the view for you behind the scenes.

on the other hand, have a lifecycle method called onCreateView which returns a view (if it has one). The most common way to do this is to inflate a view in XML and return it in this method. In this case you need to inflate it yourself though. Fragments don't have a "setContentView" method

Note:link might be destroy so answer pasted.

Community
  • 1
  • 1
M S Gadag
  • 2,057
  • 1
  • 12
  • 15
  • Are this two "view" refer to the same object in my case? – Ruiyi Luo Dec 04 '14 at 09:18
  • However, in my environment. For a API used in WelcomeLayout, when I use view2, it failed, when I call pass the view1 directly from the MainActivity, it works. I am very confused. I doubt after using the View.inflate(..), thought the context passed in are same, the view I got are different, perhaps a totally new object. – Ruiyi Luo Dec 04 '14 at 09:32
  • try like this `View v =inflate(context, R.layout.welcome_view, null); ViewGroup v2 = (ViewGroup) v.findViewById(R.id.ad_view); return v;` – M S Gadag Dec 04 '14 at 09:51
  • Yes, I have tried that, still not work. I don't know the inner mechanism of the inflate() WelcomeLayout(context) is created in the MainActivity. I am not sure the View v = View.inflate() in the WelcomeLayout will refer the same one of the MainActivity. – Ruiyi Luo Dec 04 '14 at 09:59