Let's take a look at the main interface of the browser, which indicates the corresponding classes of the browser interface, here is an example of the interface on the tablet. To sketch is to give everyone an intuitive feeling.
Browseractivity is the main interface of the entire application, the controller object is created in the OnCreate, and the controller object is the most important management class for the entire application, which is discussed later.
@Override public void OnCreate (Bundle icicle) { Mcontroller = Createcontroller ();}
A new UI class is created in the Controller, and the UI class is the primary view class, although it is not A subclass of the view class, but an interface that contains many abstract methods, but its implementation class contains important View members. These view members and the view ID one by one in the layout file in Browseractivity are described later in the UI Implementation class Baseui .
Private Controller Createcontroller () { Controller controller = new Controller (this); Boolean xlarge = Istablet (this); UI UI = null; if (XLarge) { UI = new Xlargeui (this, controller); } else { UI = new Phoneui (this, controller); } Controller.setui (UI);
From the top, we see that the values obtained from the Istablet () method will create different UI classes.
Take a look at the Istablet () method:
public static Boolean Istablet (context context) { return context.getresources (). Getboolean (R.bool.istablet); }
As you can see, this is determined by the value of a resource file, which is actually used to differentiate whether this is a mobile app or a tablet application. When the value is true, the Xlargeui object is obtained, and when the value is False, the Phoneui object is obtained . since my project is flat, take xlargeui as an example to analyze.
Here, we have a rationale for the succession of these categories:
Now let's look at the definition of Xlargeui:
public class Xlargeui extends Baseui { private ActionBar mactionbar; Private TabBar Mtabbar; Private Navigationbartablet Mnavbar; /** * @param browser * @param controller * /public Xlargeui (Activity Browser, Uicontroller Controller) { Super (browser, controller); Other code Mnavbar = (navigationbartablet) mtitlebar.getnavigationbar (); Mtabbar = new TabBar (mactivity, Muicontroller, this); Mactionbar = Mactivity.getactionbar (); Setupactionbar (); } private void Setupactionbar () { mactionbar.setnavigationmode (actionbar.navigation_mode_standard); Mactionbar.setdisplayoptions (actionbar.display_show_custom); Mactionbar.setcustomview (Mtabbar);} Other code}
Two parameters are passed in the constructor, the first is the main interface of the application browseractivity, and the second is the Uicontroller object, which mainly controls the Ui , such as the operation of a tab. , load URLs , and so on.
The main thing in the constructor is to do the following:
1. Get Navigationbartablet type Object Mnavbarby titlebar type member variable Mtitlebar, which is the navigation toolbar. is the following toolbar for the browser interface
This object is primarily used to update the state of the navigation bar, which is the move back key,URL entry box, andurl icon.
The member variable Mtitlebar is inherited from Baseui .
2 . Create a new object of type Tabbar, this Tabbar object is only a flat plate. The main interface browseractivity, Uicontroller object, Xlargeui itself are passed in when created. The created object is the tab bar
This object will be used for tabs related actions, add, delete, Update tab, change favorite icon Favicon, modify URL title, etc.
3, through the main interface browseractivity get ActionBar object.
4.set the ActionBar style and set the tab bar Tabbar object to ActionBar custom view.
About Baseui
Baseui is a common parent class between the flat interface Xlargeui and the mobile interface Phoneui .
Public abstract class Baseui implements UI {Activity mactivity; Uicontroller Muicontroller; TabControl Mtabcontrol; Private Urlbarautoshowmanager Murlbarautoshowmanager; protected TitleBar Mtitlebar; Private Navigationbarbase Mnavigationbar; protected Piecontrol Mpiecontrol; Public Baseui (Activity Browser, Uicontroller Controller) {mactivity = browser; Muicontroller = Controller; Mtabcontrol = Controller.gettabcontrol (); Framelayout framelayout = (framelayout) Mactivity.getwindow (). Getdecorview (). Findviewbyid (Android. R.id.content); Layoutinflater.from (mactivity). Inflate (R.layout.custom_screen, framelayout); ... setfullscreen (Browsersettings.getinstance (). Usefullscreen ()); Mtitlebar = new TitleBar (mactivity, Muicontroller, this, mcontentview); Mtitlebar.setprogress (100); Mnavigationbar = Mtitlebar.getnavigationbar (); Murlbarautoshowmanager = newUrlbarautoshowmanager (this);}}
Let's look at the construction method:
The construction method passed in two parameters: the first is the main interface of the application browseractivity, the second is the Uicontroller object, which is the two parameters passed in when the Xlargeui was created.
The main things to do in the construction method are as follows:
1. Get Object Mtabcontrol of TabControl type through Uicontroller object .
2. Setthe view for browseractivity. Looking at the code of Browseractivity, the entire Setcontentview shadow is not found , then how does it set the view for the activity ? It turned out to be here.
Framelayout framelayout = (framelayout) Mactivity.getwindow () . Getdecorview (). Findviewbyid (Android. r.id.content); Layoutinflater.from (mactivity). Inflate (R.layout.custom_screen, framelayout);
Here is the view that corresponds to the resource file added to Android. R.id.content defined in the framelayout . What's going on here?
The Setcontentview in the original activity are as follows:
public void Setcontentview (int layoutresid) { GetWindow (). Setcontentview (LAYOUTRESID); //... }
in Activity:
Public Window GetWindow () { return mwindow;} Mwindow = Policymanager.makenewwindow (this);
in Policymanager:
Public final class Policymanager {private static final String Policy_impl_class_name = " Com.android.internal.policy.impl.Policy "; private static final Ipolicy Spolicy; static { try { Class policyclass = Class.forName (policy_impl_class_name); Spolicy = (ipolicy) policyclass.newinstance (); } catch (Instantiationexception ex) { throw new RuntimeException ("Exception", ex); } } public static Window Makenewwindow (context context) { return Spolicy.makenewwindow (context);} }
in Ipolicy:
Public interface Ipolicy {public Window Makenewwindow (context context);
in Policy
public class Policy implements Ipolicy//...public Window Makenewwindow (context context) { return new Phonewindow ( context);} }
So the Activity 's GetWindow () gets the phonewindow object.
and Phonewindow inherited the Window, and covered the setcontentview,Phonewindow the setcontentview (int layoutresid) method is as follows:
@Override public void Setcontentview (int layoutresid) { if (mcontentparent = = null) { Installdecor (); } else { mcontentparent.removeallviews (); } Mlayoutinflater.inflate (Layoutresid, mcontentparent); Final Callback cb = Getcallback (); if (CB! = null &&!isdestroyed ()) { cb.oncontentchanged ();} }
is to fill the layout resource file into mcontentparent, so what is Mcontentparent? Look underneath .
private void Installdecor () {if (mcontentparent = = null) { mcontentparent = generatelayout (Mdecor);//...}} Protected ViewGroup generatelayout (Decorview decor) {//... ViewGroup contentparent = (viewgroup) Findviewbyid (id_android_content);//...return contentparent;}
From the above, it is defined by a resource file with ID id_android_content .
This value is inherited by the Window class and looks at the definition
public static final int id_android_content = com.android.internal.r.id.content;
Mactivity.getwindow ()get aWindowobject that is called by theGetdecorview ()found theAndroid. R.id.content, it is here that thecom.android.internal.r.id.content, soActivityof theSetcontentviewThe reality is toViewadded toAndroid. R.id.contentdefined byViewGroup, the second step above is equivalent to theIn browseractivitySetcontentview.
3 . Set whether full screen according to the configuration value in Browsersettings.
4 titlebar baseui framelayout, The first two parameters are as baseui The parameters of the construction method are passed in, and the third one is baseui in a layout. framelayout titlebar Span style= "Font-family:consolas" >tabbar
5.set the maximum value of ProgressBar in titlebar . This ProgressBar also shows the progress of loading the page. appears when loaded and disappears when it is loaded.
6, obtains the navigationbarbase object, obtains in the tablet is the Navigationbartablet, obtains in the handset is navigationbarphone. that is, the navigation bar.
7.Create a urlbarautoshowmanager object that controls the display and hiding of the title bar titlebar during page scrolling .
Looking back at Xlargeui, we mentioned objects Mnavbar and Tabbar of type Navigationbartablet, why aren't these two defined in baseui ?
This is because these two are unique in the tablet interface and do not exist in the phone interface. Mnavbar in the mobile interface is converted to Navigationbarphone type, and Tabbar is the tab bar, the phone screen is small, all have no tab bar.