Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 更簡單更全的material design狀態欄

更簡單更全的material design狀態欄

編輯:關於Android編程

從實際使用需要出發,以最簡單的方式實現了幾種類型的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:



    

    

.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:

\\

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved