Android and androidsdk

Source: Internet
Author: User
Tags xml attribute

Android and androidsdk
DecorView

During development, setContentView (R. layout. custom_layout) is usually called in onCreate () to implement the desired page layout. The page is attached to the window, and the DecorView is the top view of the window. In Android frameworks, classes related to Window view processing are mainly Window and Its Implementation class PhoneWindow.

Public class PhoneWindow extends Window implements MenuBuilder. callback {//... // View private DecorView mDecor on the top of the window; // The Root View of all custom views, id = "@ android: id/content" private ViewGroup mContentParent ;//...}

DecorView is actually an internal class in PhoneWindow and is essentially a View. It only extends the implementation of FrameLayout.

private final class DecorView extends FrameLayout implements RootViewSurfaceTaker
Add to Window Process

Public void setContentView (int layoutResID) {// getWindow () obtains the PhoneWindow object getWindow (). setContentView (layoutResID );}

Let's take a look at the window class.

Public abstract class Window {//... // specify the style type of the Activity window public static final int FEATURE_NO_TITLE = 1; public static final int FEATURE_INDETERMINATE_PROGRESS = 5; // set the layout file public abstract void setContentView (int layoutResID ); public abstract void setContentView (View view); // request public boolean requestFeature (int featureId) {final int flag = 1 <featureId; mFeatures | = flag; mLocalFe Atures | = mContainer! = Null? (Flag &~ MContainer. mFeatures): flag; return (mFeatures & flag )! = 0 ;}//...}
  • Execute setContentView (int layoutResource) in PhoneWindow)

The PhoneWindow class inherits from the Window class and is the specific implementation of the Window class, that is, we can draw a Window through this class. In addition, this class contains a DecorView object, which is the root View of all application windows (Activity interfaces. In short, the PhoneWindow class encapsulates a FrameLayout class, namely, the DecorView object, as the Root View of the application window, and provides a set of common window operation interfaces.

Public void setContentView (int layoutResID) {// initial, mContentParent is empty if (mContentParent = null) {installDecor ();} else {mContentParent. removeAllViews ();} // inflate custom layout and use mContentParent as its root view mLayoutInflater. inflate (layoutResID, mContentParent );//...}

This method first determines whether the mContentParent is obtained by setContentView (), that is, whether the setContentView () method of the PhoneWindow object is called for the first time. If this is the first call, the installDecor () method is called. Otherwise, all child views in the mContentParent are removed. Finally, convert our resource files to the View tree through the LayoutInflater object and add them to the mContentParent View (in the application, we can call setContentView () multiple times () to display our interface .).

  • PhoneWindow. installDecor ()

 

Private void installDecor () {if (mDecor = null) {// new A DecorView mDecor = generateDecor (); mDecor. setDescendantFocusability (ViewGroup. FOCUS_AFTER_DESCENDANTS); mDecor. setIsRootNamespace (true); if (! MInvalidatePanelMenuPosted & mInvalidatePanelMenuFeatures! = 0) {mDecor. postOnAnimation (mInvalidatePanelMenuRunnable) ;}} if (mContentParent = null) {// This step sets the window modification file, and assign the view find with id ID_ANDROID_CONTENT to mContentParent = generateLayout (mDecor) as the return value );//...}

 

  • PhoneWindow. generateLayout (DecorView decor)

 

Protected ViewGroup generateLayout (DecorView decor) {// 1, get the themes or code requestWindowFeature () specified by the <Application android: theme = ""/>, <Activity/> node () and set TypedArray a = getWindowStyle ();//... // 2. Obtain the window Features and set the corresponding modified layout files. These xml files are located in int layoutResource under frameworks/base/core/res/layout; int features = getLocalFeatures (); if (features & (1 <FEATURE_LEFT_ICON) | (1 <FEATURE_RIGHT_ICON )))! = 0) {if (mIsFloating) {TypedValue res = new TypedValue (); getContext (). getTheme (). resolveAttribute (com. android. internal. r. attr. dialogTitleIconsDecorLayout, res, true); layoutResource = res. resourceId;} else {layoutResource = com. android. internal. r. layout. screen_title_icons;} removeFeature (FEATURE_ACTION_BAR);} else if (features & (1 <FEATURE_PROGRESS) | (1 <FEATURE_INDETERMINATE_PROGRE SS )))! = 0 & (features & (1 <FEATURE_ACTION_BAR) = 0) {layoutResource = com. android. internal. r. layout. screen_progress ;//... mDecor. startChanging (); // 3. Set the layout file inflate as the View tree and add it to View in = mLayoutInflater in decorView. inflate (layoutResource, null); decor. addView (in, new ViewGroup. layoutParams (MATCH_PARENT, MATCH_PARENT); // assign the View of id = "@ android: id/content" in the window decoration layout file to mContentParent, subsequent custom views and layout will all be their child ViewGroup contentParent = (ViewGroup) findViewById (ID_ANDROID_CONTENT); if (contentParent = null) {throw new RuntimeException ("Window couldn't find content container view ");}//...}

This method will do the following:

Select different window layout files (Root View) for the window based on the style modification type ). These window decoration layout files specify a ViewGroup view used to store the Activity custom Layout file. Generally, the id of FrameLayout is: android: id = "@ android: id/content ".

For example, the window modifier types include FullScreen (full screen) and NoTitleBar (excluding the title bar. There are two types of window modifier selected:

① Specify requestFeature () to specify the window modifier. The PhoneWindow object calls the getLocalFeature () method to obtain the value;

② Configure the corresponding attribute for our Activity, that is, android: theme = "". The PhoneWindow object calls the getWindowStyle () method to obtain the value.

For example, you can use the following method to hide a title bar:

requestWindowFeature(Window.FEATURE_NO_TITLE);

Or configure the xml Attribute for the Activity:

android:theme="@android:style/Theme.NoTitleBar"

Therefore, the requestFeature () method must be called before setContentView in the Activity.

After determining the window style, select the layout files corresponding to the style. These layout files are located in frameworks/base/core/res/layout /,

Typical window layout files include:

R. layout. dialog_titile_icons R. layout. screen_title_iconsR.layout.screen_progress R. layout. dialog_custom_titleR.layout.dialog_title R. layout. screen_title // The most common Activity window modifier layout file R. layout. screen_simple // full screen Activity window layout File
  • The custom layout set on the last page will be added to mContentParent.
mLayoutInflater.inflate(layoutResID, mContentParent);

The whole process mainly involves how to add the layout file of the Activity to the window. The above process can be summarized:

Finally, when AMS (ActivityManagerService) prepares a resume Activity, it calls back the handleResumeActivity () method of the Activity. This method calls the makeVisible method of the Activity, the mDecor view family we just created is displayed.

// When the system resume an Activity, call this method final void handleResumeActivity (IBinder token, boolean clearHide, boolean isForward) {ActivityRecord r = javasmresumeactivity (token, clearHide );//... if (r. activity. mVisibleFromClient) {r. activity. makeVisible ();}}
Void makeVisible () {if (! MWindowAdded) {ViewManager wm = getWindowManager (); // obtain the WindowManager object wm. addView (mDecor, getWindow (). getAttributes (); mWindowAdded = true;} mDecor. setVisibility (View. VISIBLE); // make it display}
Layout hierarchy

@ Overrideprotected void onCreate (Bundle savedInstanceState) {// set the Window without the title bar requestWindowFeature (Window. Temperature); super. onCreate (savedInstanceState); setContentView (R. layout. activity_decor );}

Activity_decor.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    tools:context=".DecorActivity">    <TextView        android:text="@string/hello_world"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:text="@string/hello_world"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"/></RelativeLayout>
 

The Window modification layout file corresponding to Window. FEATURE_NO_TITLE set in onCreate () is screen_simple.xml. The source code is as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:fitsSystemWindows="true"    android:orientation="vertical">    <ViewStub android:id="@+id/action_mode_bar_stub"              android:inflatedId="@+id/action_mode_bar"              android:layout="@layout/action_mode_bar"              android:layout_width="match_parent"              android:layout_height="wrap_content" />    <FrameLayout         android:id="@android:id/content"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:foregroundInsidePadding="false"         android:foregroundGravity="fill_horizontal|top"         android:foreground="?android:attr/windowContentOverlay" /></LinearLayout>

In the source code, FrameLayout with the id "@ android: id/content" is the content area. In the whole process, it will assign the value to the property mContentParent in the PhoneWindow class. After the application is run, use the hierarchyviewer tool provided by the SDK to view the ViewTree structure of the page. The structure is as follows:

I am the dividing line of tiantiao

 

 

Reference: http://www.cnblogs.com/yogin/p/4061050.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.