简体   繁体   中英

Inflate a View Programmatically

How can I inflate a view programmatically?

I'm trying to implement a NavigationDrawer in Android and I have a main view named HomeworkMain with a superclass of Activity.

I also have a HomeworkListActivity which calls:

@Override
protected Fragment createFragment() {
    return new HomeworkListFragment();
}

to create a HomeworkListFragment.

I want HomeworkListActivity to be the root view of my HomeworkMain. But it doesn't have any xml file attached to it as you can see since it just calls HomeworkListFragment.

So how do I put this into the onCreateView method and inflate the view?

You can inflate your own layout like below. try this one first.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.item, container, false);
    titleTxt = (TextView)   view.findViewById(R.id.title);
    return view;
}

If this is wrong, plz let me know more specific.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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