if(Findviewbyid (Android). R.id.content)instanceofViewGroup) {ViewGroup MainView=( (ViewGroup) Findviewbyid (Android. R.id.content)); if(Mainview.getchildcount ()! = 0) {(ViewGroup) Mainview.getchildat (0) . AddView (Quitbutton); } Else{Mainview.addview (Quitbutton); } quitbutton.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {onbackpressed (); } }); }
What the code does is get the content layout and then add a button to the view.
Findviewbyid (Android. r.id.content) Gets the parent view of the root view, which is the bottom-most view. The root view is its first and possibly the only child.
The first thing you need to see is whether content layout is ViewGroup, because the root view may be just a view instead of a viewgroup. In practice, the root view is set as relativelayout as possible, so that it is convenient to add controls wherever they are needed.
Android Get Content Layout