編輯:關於android開發
從實際使用需要出發,以最簡單的方式實現了幾種類型的MD狀態欄。(重點在fitsSystemWindows的使用)
0,使用前提
Theme.AppCompat.Light.DarkActionBar
targetSdkVersion 23;
support librariy 23.3.0
styles-v19:
styles-v21+:
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:
.javawindows="true" android:layout_height="wrap_content" />
/** * 圖片全屏透明狀態欄(圖片位於狀態欄下面) * * @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.4上CollapsingToolbar折疊時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:
Android的幾個主要組件可以相互協調工作,共同組成一個完整的Android
自動化運維之cobbler批量部署操作系統(一)注:本文只介紹cobbler簡單的安裝和使用,進階操作以後有時間再進行整理。cobbler裝機系統是較早前kickstar
ListView中響應item的點擊事件並且刷新界面,listviewitem---恢復內容開始--- 最近在在實現listview功能中遇到了這個問題: 點擊事件寫在了
一些常用的UI控件,常用UI控件讓你的 EditText 全部清除: 網址:https://github.com/MrFuFuFu/ClearEditText
Android開發自學筆記(Android Studio)—