Android Transparent status bar method and its adaptation keyboard push (II)

Source: Internet
Author: User

In the previous article, we introduced a way to set the transparency status bar and how to fit it on a keyboard. But one of the drawbacks of the previous introduction is that it is not possible to eliminate the shadow of StatusBar. Many mobile phones (Samsung, nexus are shaded). Even if I used:

<android.support.design.widget.appbarlayout       android:layout_width= "match_parent"       android:layout_ height= "Wrap_content"       app:elevation= "0DP"       android:background= "@android: Color/transparent"       android: Theme= "@style/apptheme.appbaroverlay" >       <android.support.v7.widget.toolbar           android:id= "@+id/ Toolbar "           android:layout_width=" match_parent "           android:layout_height="? Attr/actionbarsize "           android: Background= "@android: color/transparent"           app:popuptheme= "@style/apptheme.popupoverlay"/>           </ Android.support.design.widget.appbarlayout>

Still can't eliminate the shadow. What about that? Here is a good way I think, the idea is: do not toolbar, directly through the code to set the interface for transparent StatusBar, in each interface to add a StatusBar height view to adapt to the display of the interface, Otherwise, the top of the interface will be partially covered by statusbar.

Here's how:

1. Modify the top-most layout of each interface in your application:

    <relativelayout        android:id= "@+id/total_tile_id"        android:layout_width= "Match_parent"        android: layout_height= "Wrap_content" >        <relativelayout            android:id= "@+id/actionbar_tile_bg"            android: Layout_width= "Match_parent"            android:layout_height= "wrap_content" >        </RelativeLayout>       <! Other layouts for--titlebar--></relativelayout>

2. In the activity's OnCreate method (preferably written in baseactivity), add the following code, set to transparent statusbar and clear the background shadow.

            GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS),//transparent status bar            //status bar font set to dark, system_ui_ Flag_light_status_bar adds            GetWindow () to SDK23. Getdecorview (). Setsystemuivisibility (View.system_ui_flag_layout_            System_bar_backgrounds);            GetWindow (). Clearflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);            GetWindow (). Setstatusbarcolor (color.transparent);//SDK21

  

3. Also in the OnCreate method of activity (preferably written in baseactivity), call the Initmargintopwithstatusbarheight method

/**     * Set the margintop height of the view to StatusBar height     * @param view is the ACTIONBAR_TILE_BG of the above view     * @param context     */ public static void Initmargintopwithstatusbarheight (View view,context Context) {        if (Build.VERSION.SDK_INT >= Build.version_codes. M && context = null) {            view.setvisibility (view.visible);            Relativelayout.layoutparams RL = new Relativelayout.layoutparams (View.getlayoutparams ());            Rl.topmargin = getstatusbarheight (context);            View.setlayoutparams (RL);        } else{            view.setvisibility (view.gone);        }    }

This can achieve more than 6.0 of the model StatusBar is transparent and no shadow (6.0 of the test is not tested, I this application is required to require more than 6.0, interested students can go to test).

However, after this set up, with the previous article on the keyboard to push or the virtual key display and hide adaptation is not necessary, need to reset.

Add a Util class:

public class Androidbug5497workaround {//For more information, see Https://code.google.com/p/android/issues/detail?id    =5497//to the use of this class, simply invoke Assistactivity () on the Activity that already have its content view set.    public static void Assistactivity (final activity activity) {new androidbug5497workaround (activity);    } private View mchildofcontent;    private int usableheightprevious;    Private Framelayout.layoutparams Framelayoutparams; Private Androidbug5497workaround (final activity activity) {if (Build.VERSION.SDK_INT >= build.version_codes. M) {Framelayout content = (framelayout) Activity.findviewbyid (Android.            R.id.content);            mchildofcontent = Content.getchildat (0);                Mchildofcontent.getviewtreeobserver (). Addongloballayoutlistener (New Viewtreeobserver.ongloballayoutlistener () {                public void Ongloballayout () {possiblyresizechildofcontent (activity);         }   });        Framelayoutparams = (framelayout.layoutparams) mchildofcontent.getlayoutparams (); }} private void Possiblyresizechildofcontent (activity activity) {int usableheightnow = Computeusableheight (        ); if (usableheightnow! = usableheightprevious) {int resourceId = Activity.getresources (). Getidentifier ("Navigati            On_bar_height "," Dimen "," Android ");            Gets the height of navigationbar int navigateheight = Activity.getresources (). Getdimensionpixelsize (ResourceId);            Boolean Hassoftnavigatebar = NavigationBarExist2 (activity);            int usableheightsanskeyboard = Mchildofcontent.getrootview (). GetHeight ();            int heightdifference = Usableheightsanskeyboard-usableheightnow;                     if (Heightdifference > (USABLEHEIGHTSANSKEYBOARD/4) | | hassoftnavigatebar) {if (heightdifference = = 0) {                Heightdifference = Navigateheight; }//keyboard probably just became visible framelayoutparams.height = usableheightsanskeyboard-heightdifference; } else {//keyboard probably just became hidden framelayoutparams.height = Usableheightsans            Keyboard;            } mchildofcontent.requestlayout ();        usableheightprevious = Usableheightnow;        }} private int computeusableheight () {rect r = new Rect ();        Mchildofcontent.getwindowvisibledisplayframe (R);    return r.bottom;  }/** * Determines if a virtual key is used, this method works best * @param activity * @return */Private Boolean navigationBarExist2 (activity        Activity) {WindowManager WindowManager = Activity.getwindowmanager ();        Display d = windowmanager.getdefaultdisplay ();        Displaymetrics realdisplaymetrics = new Displaymetrics (); if (Build.VERSION.SDK_INT >= build.version_codes.        JELLY_BEAN_MR1) {d.getrealmetrics (realdisplaymetrics); } int realheight = ReaLdisplaymetrics.heightpixels;        int realwidth = Realdisplaymetrics.widthpixels;        Displaymetrics displaymetrics = new Displaymetrics ();        D.getmetrics (Displaymetrics);        int displayheight = Displaymetrics.heightpixels;        int displaywidth = Displaymetrics.widthpixels; Return (Realwidth-displaywidth) > 0 | |    (realheight-displayheight) > 0; }}

In the activity OnCreate call:

        Androidbug5497workaround.assistactivity (this);

  

OK, these two are the problems I encountered during the development process, recorded and shared.


  

Android Transparent status bar method and its adaptation keyboard push (II)

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.