Implement the Android K pretend immersive, android pretend immersive

Source: Internet
Author: User
Tags getcolor

Implement the Android K pretend immersive, android pretend immersive

After Android 5.0, the MD style was introduced, and the status bar immersion became a design habit. The Android system that stays before Android L cannot directly implement immersion. Here we will introduce how to implement the hypothetical immersion of the Android K Series.

For immersive effects, I 'd like to paste a few pictures here.

 

Android L

 

For Android 4.2

 

In the Android K Series, the status bar shows the gradient effect.

The following describes how to implement the effects. Here, we will not go into details about the implementation of the immersive effects of Android L or above. You can configure several attributes in xml.

 

Digress: This article describes how to use Android Studio to develop Android as an IDE. If other ides are used, set or configure xml files according to relevant settings.

 

Start learning pretend immersion

The so-called other knowledge is to create a base class BaseActivity shared by the Activity in other apps, so that it inherits the AppCompatActivity, and then implement the pretend immersion in the base class, in this way, Acticity can focus on its own content and hand over these common settings to BaseActivity for processing.

Now let's take a look at how to deal with this part, first look at the Code:

 1 public class  BaseActivity extends AppCompatActivity { 2  3     private int mColor; 4      5     public BaseActivity(){ 6     } 7  8     public BaseActivity(int color){ 9         super();10         mColor = color;11     }12 13     @Override14     protected void onCreate(@Nullable Bundle savedInstanceState) {15         super.onCreate(savedInstanceState );16         StatusBarCompat.compat(this, ContextCompat.getColor(this, mColor));17     }18 19     public static class StatusBarCompat{20 21         private static final int INVALID_VAL = -1;22         private static final int COLOR_DEFAULT = Color.parseColor("#20000000");23 24         public static  void compat(Activity activity, int statusColor){25             if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){26                 if(statusColor != INVALID_VAL){27                     activity.getWindow().setStatusBarColor(statusColor);28                 }29                 return;30             }31 32             if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT33                     && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){34                 int color = COLOR_DEFAULT;35                 ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);36 37                 if(statusColor != INVALID_VAL){38                     color = statusColor;39                 }40 41                 View statusBarView = new View(activity);42                 ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,43                         getStatusBarHeight(activity));44                 statusBarView.setBackgroundColor(color);45 46                 contentView.addView(statusBarView, lp);47                 //contentView.addView(statusBarView, 0, lp);48             }49         }50 51         public static void compat(Activity activity){52             compat(activity, INVALID_VAL);53         }54 55         public static int getStatusBarHeight(Context context){56             int result = 0;57 58             int resourceId = context.getResources().getIdentifier("status_bar_height",59                     "dimen", "android");60 61             if(resourceId > 0){62                 result = context.getResources().getDimensionPixelSize(resourceId);63             }64 65             return result;66         }67     }68 }

 

Follow the steps, there is no need to look at all those messy code. Here we will explain the important part. In BaseActivity, we provide a single parameter constructor to let the subclass choose the colors that pretend to be immersed, the onCreate function calls StatusBarCompat. compat (this, ContextCompat. for the getColor (this, mColor) function, StatusBarCompat is its internal class. Let's look at how the compat function achieves pretend immersion.

 

The first if function is used to determine whether the system is Android L or above, then the setStatusBarColor function is called directly. This function is provided after API 21 to set the status bar color, then the second if is used to determine whether it is an Android K system. if so, start our step. The content is actually like this. We configure the background of the status bar in XML to be transparent, and insert a View like the status bar in our layout, this visually seems like an immersive effect. It is literally easy to understand the meaning of this, and then analyze the feasibility. First, there are many ways to get our layout. You can set an id for the root layout, and then Activity. findViewById directly gets the root layout. In fact, you can also choose not to get the layout here. You can set a View in xml, and then obtain the View here and set its height to the height of the status bar, the background is the color from the Activity. Here we will not introduce these two methods. We will introduce one method to obtain our root layout. Code 35 lines findViewById (android. r. id. content) Get a FrameLayout, and then it's strange, why is it a FrameLayout? In fact, our layout is set in the FrameLayout layout of AppCompatActivity, this code is used to obtain the content of this ViewGroup. Therefore, we have mastered the ViewGroup of the entire Activity, and our operations will be conducted here. After obtaining the true root layout, of course, you can create a View, make the height consistent with the status bar height, and then set it to the obtained ViewGroup. The getStatusBarHeight function is used to obtain the height of the system status bar, set the background color of the View, and add it to the ViewGroup to complete the entire operation. Now the work is not completed yet, since it is the Android K machine, first configure XML, In the res directory to create a folder for the values-v19, then create a new styles. xml file, paste the following code:

1 <?xml version="1.0" encoding="utf-8"?>2 <resources>3     <style name="AppTheme" parent="@style/BaseAppTheme">4         <item name="android:windowTranslucentStatus">true</item>5     </style>6 </resources>

Take a look at the BaseAppTheme code. This part of the code is in res/values/styles:

1 <style name="BaseAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">2         <!-- Customize your theme here. -->3         <item name="colorPrimary">@color/colorPrimary</item>4         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>5         <item name="colorAccent">@color/colorAccent</item>6 </style>

The android: windowTranslucentStatus attribute sets whether the status bar is set to transparent. This attribute is introduced only after v19.

It seems that the problem has been completed, but the problem has not been completely solved, that is, the Space Location of the ToolBar. If not,

We can see that the ToolBar and the status bar are crowded together, but this is not the result we want in this article. What should we do? In fact, you only need to set a ToolBar attribute android: fitsSystemWindows = "true. This attribute makes the padding corresponding to the view setting of this attribute free up the corresponding space for the status bar. To put it bluntly, it is like paddingTop = statusHeight.

After setting the parameters by step, there is another step. Otherwise, we will see the default color set in the Code. Therefore, we need to pass the color to BaseActivity in the Activity. The simple code is as follows:

1 public MainActivity(){2     this(R.color.colorPrimary);3 }4 5 private MainActivity(int color){6     super(color);7 }

You only need to modify R. color. colorPrimary to the color you need. The reason for this operation is very simple. Because no parameter constructor is called by default, you can call the constructor without parameters, this also explains why the parent class should define this parameter structure.

 

After running the command, the effect is good. But if you use the view added in this method as the background color of the status bar, there is actually a pitfall, however, the code I posted already explains the solution to this pitfall. This pitfall is actually used with DrawerLayout. When you open the drawer, you will see the slide menu below the status bar ,:

As you can see, the status bar blocks the slide menu. To put it bluntly, the view we added blocks the status bar. How can we solve this problem? I noted a sentence in the code above. In fact, using the three-parameter function below can solve this problem,

It can be seen that the slide menu has been covered in the view that pretends to be the status bar. The reason for this is very simple. When using the first function, it is directly added to the top of FrameLayout, so it is blocked in our layout, while addView (statusBar, 0, lp) means to insert it to the position of the 1st views, so it will be inserted under our layout, So we achieve the desired effect. Since there are successive problems with the two parameters, can we perform this operation in the onCreat function of the Activity, for example, first super. onCreat then calls setContentView. It seems that our layout should be on the view that pretends to be the status bar background. In fact, this is a pitfall, in onCreat, we must first call setContentView and then call the onCreat of the base class. Otherwise, the status bar Effect

In fact, the setContentView name shows why. Since this is a custom layout, it is clear that the content in FrameLayout will be cleared when calling this function, therefore, before calling this function, add

The view has been cleared.

 

Here, the hypothetical immersive Effect of Android K is achieved.

 

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.