更簡單更全的material design狀態列

來源:互聯網
上載者:User

更簡單更全的material design狀態列

從實際使用需要出發,以最簡單的方式實現了幾種類型的MD狀態列。(重點在fitsSystemWindows的使用)

0,使用前提

Theme.AppCompat.Light.DarkActionBar

targetSdkVersion 23;

support librariy 23.3.0

styles-v19:true

styles-v21+:true @android:color/transparent

1,最普通的類型:只有一個ToolBar

layout:

 

                

 

.java:

 

/** * 簡單型狀態列(ToolBar) * * @param activity */public static void setOrdinaryToolBar(Activity activity) {    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {        activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, R.color.colorPrimaryDark));    } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {        setKKStatusBar(activity, R.color.colorPrimaryDark);    }}
效果:

 

左:Android4.4 右:Android6.0

 

 

2,圖片全屏透明狀態列(圖片位於狀態列下面)

layout:

        windows="true" android:layout_height="wrap_content" />
.java

 


/**  * 圖片全屏透明狀態列(圖片位於狀態列下面)  *  * @param activity  */ public static void setImageTransparent(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); } }

 

效果:

左:Android4.4 右:Android6.0


 

3,圖片全屏半透明狀態列(圖片位於狀態列下面)

layout:

 

        
.java:

 

 

/** * 圖片全屏半透明狀態列(圖片位於狀態列下面) * * @param activity */public static void setImageTranslucent(Activity activity) {    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {        activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);        activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, R.color.statusBar));    } else {        setKKStatusBar(activity, R.color.statusBar);    }}

 

效果:

左:Android4.4 右:Android6.0


 

 

4,ToolBar+TabLayout狀態列(ToolBar可伸縮)

layout:

 

                             
.java:

 

 

/** * ToolBar+TabLayout狀態列(ToolBar可伸縮) * * @param activity */public static void setToolbarTabLayout(Activity activity) {    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {        activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, R.color.colorPrimaryDark));    }}

 

效果:

Android4.4:


Android6.0:

 

5,DrawerLayout+ToolBar+TabLayout狀態列(ToolBar可伸縮)

layout:

                                                            
.java:

 

/** * DrawerLayout+ToolBar+TabLayout狀態列(ToolBar可伸縮) * * @param activity * @param drawerLayout * @param coordinatorLayout */public static void setDrawerToolbarTabLayout(Activity activity, CoordinatorLayout coordinatorLayout) {    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {        ViewGroup contentLayout = (ViewGroup) activity.findViewById(android.R.id.content);        contentLayout.getChildAt(0).setFitsSystemWindows(false);        coordinatorLayout.setFitsSystemWindows(true);        setKKStatusBar(activity, R.color.statusBar);    }}

 

效果:

Android4.4:


Android6.0:

 

 

6,CollapsingToolbarLayout狀態列(可摺疊圖片)

layout:

                                                     
.java:

 

/** * CollapsingToolbarLayout狀態列(可摺疊圖片) * * @param activity * @param coordinatorLayout * @param appBarLayout * @param imageView * @param toolbar */public static void setCollapsingToolbar(Activity activity, CoordinatorLayout coordinatorLayout,                                        AppBarLayout appBarLayout, ImageView imageView, Toolbar toolbar) {    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {        coordinatorLayout.setFitsSystemWindows(false);        appBarLayout.setFitsSystemWindows(false);        imageView.setFitsSystemWindows(false);        toolbar.setFitsSystemWindows(true);        CollapsingToolbarLayout.LayoutParams lp = (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams();        lp.height = (int) (getStatusBarHeight(activity) +                activity.getResources().getDimension(R.dimen.abc_action_bar_default_height_material));        toolbar.setLayoutParams(lp);        setKKStatusBar(activity, R.color.statusBar);        setCollapsingToolbarStatus(appBarLayout);    }}

 

效果:

Android4.4:


Android6.0:

 

 

7,DrawerLayout+ToolBar型狀態列

layout:

                    
.java:

 

/** * DrawerLayout+ToolBar型狀態列 * * @param activity */public static void setDrawerToolbar(Activity activity) {    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {        ViewGroup contentLayout = (ViewGroup) activity.findViewById(android.R.id.content);        contentLayout.getChildAt(0).setFitsSystemWindows(false);        setKKStatusBar(activity, R.color.statusBar);    }}

 

其它:

 

/** * Android4.4CollapsingToolbar摺疊時statusBar顯示和隱藏 * * @param appBarLayout */private static void setCollapsingToolbarStatus(AppBarLayout appBarLayout) {    ViewCompat.setAlpha(mStatusBarView, 1);    appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {        @Override        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {            int maxScroll = appBarLayout.getTotalScrollRange();            float percentage = (float) Math.abs(verticalOffset) / (float) maxScroll;            ViewCompat.setAlpha(mStatusBarView, percentage);        }    });}private static void setKKStatusBar(Activity activity, int statusBarColor) {    ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);    mStatusBarView = contentView.getChildAt(0);    //改變顏色時避免重複添加statusBarView    if (mStatusBarView != null && mStatusBarView.getMeasuredHeight() == getStatusBarHeight(activity)) {        mStatusBarView.setBackgroundColor(ContextCompat.getColor(activity, statusBarColor));        return;    }    mStatusBarView = new View(activity);    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,            getStatusBarHeight(activity));    mStatusBarView.setBackgroundColor(ContextCompat.getColor(activity, statusBarColor));    contentView.addView(mStatusBarView, lp);}private static int getStatusBarHeight(Context context) {    int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");    return context.getResources().getDimensionPixelSize(resourceId);}

 

效果:

Android4.4:


Android6.0:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.