Android: setContentView() == getViewInflate().inflate()?

浪尽此生 提交于 2019-12-03 18:27:19

The setContentView on your Activity actually calls the setContentView on the Window used by the activity, which itself does a lot more than just inflating the layout.

What you could do is to map the views to the class field using reflexion. You can download a utility class on Github that does this.

It will parse all the views declared in the layout, then try to find the name corresponding to the id in your R.id class. Then it will try to find a field with the same name in the target object and set it with the corresponding view.

For example, if you have a layout like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
</LinearLayout>

it will automatically map it to a textView1 field in your activity.

I'm posting my poor research. In summary, Activity.setContentView() delegates PhoneWindow.setContentView() (the only concrete class of Window ) within which LayoutInflater.inflate() is called, so saying "setContentView() == ViewInflate().inflate()" is not so overly off, I guess.

public class Activity extends {

    private Window mWindow;

    public void setContentView(int layoutResID) {
        getWindow().setContentView(layoutResID);
        initActionBar();
    }

    public Window getWindow() {
        return mWindow;
    }
}



public class PhoneWindow extends Window {

    private LayoutInflater mLayoutInflater;

    @Override
    public void setContentView(int layoutResID) {
        if (mContentParent == null) {
            installDecor();
        } else {
            mContentParent.removeAllViews();
        }
        **mLayoutInflater.inflate(layoutResID, mContentParent);**
        final Callback cb = getCallback();
        if (cb != null) {
            cb.onContentChanged();
        }
    }
}

Actually you're right, there's two ways to achieve the same thing:

1) setContentView(R.layout.layout);

2)

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View v = inflater.inflate(R.layout.layout, null);
setContentView(v);

You decide what is more appropriate for you. Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!