首页 / ANDROID / Android 沉浸模式
Android 沉浸模式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android 沉浸模式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含17036字,纯文字阅读大概需要25分钟。
内容图文
![Android 沉浸模式](/upload/InfoBanner/zyjiaocheng/1173/e8cb78c100d848c1913804ec52ffa575.jpg)
---恢复内容开始---
当Android系统版本大于19(4.4),就可以开启沉浸式标题栏:
可以将其封装成方法进行调用。
1 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 2 Window win = getWindow(); 3 4 WindowManager.LayoutParams winParams = win.getAttributes(); 5 winParams.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; 6 win.setAttributes(winParams); 7 8 SystemBarTintManager tintManager = new SystemBarTintManager(this); 9 tintManager.setStatusBarTintEnabled(true); 10 tintManager.setStatusBarTintColor(Color.TRANSPARENT); 11 }
// 沉浸式状态栏
其第三方源代码:
1 /** 2 * Class to manage status and navigation bar tint effects when using KitKat 3 * translucent system UI modes. 4 * 5 */ 6 public class SystemBarTintManager { 7 8 static { 9 // Android allows a system property to override the presence of the navigation bar. 10 // Used by the emulator. 11 // See https://github.com/android/platform_frameworks_base/blob/master/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java #L1076 12 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 13try { 14 Class c = Class.forName("android.os.SystemProperties"); 15 Method m = c.getDeclaredMethod("get", String.class); 16 m.setAccessible(true); 17 sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys"); 18 } catch (Throwable e) { 19 sNavBarOverride = null; 20 } 21 } 22 } 23 24 25/** 26 * The default system bar tint color value. 27*/ 28publicstaticfinalint DEFAULT_TINT_COLOR = 0x99000000; 29 30privatestatic String sNavBarOverride; 31 32privatefinal SystemBarConfig mConfig; 33privateboolean mStatusBarAvailable; 34privateboolean mNavBarAvailable; 35privateboolean mStatusBarTintEnabled; 36privateboolean mNavBarTintEnabled; 37private View mStatusBarTintView; 38private View mNavBarTintView; 39 40/** 41 * Constructor. Call this in the host activity onCreate method after its 42 * content view has been set. You should always create new instances when 43 * the host activity is recreated. 44 * 45 * @param activity The host activity. 46*/ 47 @TargetApi(19) 48public SystemBarTintManager(Activity activity) { 49 50 Window win = activity.getWindow(); 51 ViewGroup decorViewGroup = (ViewGroup) win.getDecorView(); 52 53if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 54// check theme attrs 55int[] attrs = {android.R.attr.windowTranslucentStatus, 56 android.R.attr.windowTranslucentNavigation}; 57 TypedArray a = activity.obtainStyledAttributes(attrs); 58try { 59 mStatusBarAvailable = a.getBoolean(0, false); 60 mNavBarAvailable = a.getBoolean(1, false); 61 } finally { 62 a.recycle(); 63 } 64 65// check window flags 66 WindowManager.LayoutParams winParams = win.getAttributes(); 67int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; 68if ((winParams.flags & bits) != 0) { 69 mStatusBarAvailable = true; 70 } 71 bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION; 72if ((winParams.flags & bits) != 0) { 73 mNavBarAvailable = true; 74 } 75 } 76 77 mConfig = new SystemBarConfig(activity, mStatusBarAvailable, mNavBarAvailable); 78// device might not have virtual navigation keys 79if (!mConfig.hasNavigtionBar()) { 80 mNavBarAvailable = false; 81 } 82 83if (mStatusBarAvailable) { 84 setupStatusBarView(activity, decorViewGroup); 85 } 86if (mNavBarAvailable) { 87 setupNavBarView(activity, decorViewGroup); 88 } 89 90 } 91 92/** 93 * Enable tinting of the system status bar. 94 * 95 * If the platform is running Jelly Bean or earlier, or translucent system 96 * UI modes have not been enabled in either the theme or via window flags, 97 * then this method does nothing. 98 * 99 * @param enabled True to enable tinting, false to disable it (default). 100*/101publicvoid setStatusBarTintEnabled(boolean enabled) { 102 mStatusBarTintEnabled = enabled; 103if (mStatusBarAvailable) { 104 mStatusBarTintView.setVisibility(enabled ? View.VISIBLE : View.GONE); 105 } 106 } 107108/**109 * Enable tinting of the system navigation bar. 110 * 111 * If the platform does not have soft navigation keys, is running Jelly Bean 112 * or earlier, or translucent system UI modes have not been enabled in either 113 * the theme or via window flags, then this method does nothing. 114 * 115 * @param enabled True to enable tinting, false to disable it (default). 116*/117publicvoid setNavigationBarTintEnabled(boolean enabled) { 118 mNavBarTintEnabled = enabled; 119if (mNavBarAvailable) { 120 mNavBarTintView.setVisibility(enabled ? View.VISIBLE : View.GONE); 121 } 122 } 123124/**125 * Apply the specified color tint to all system UI bars. 126 * 127 * @param color The color of the background tint. 128*/129publicvoid setTintColor(int color) { 130 setStatusBarTintColor(color); 131 setNavigationBarTintColor(color); 132 } 133134/**135 * Apply the specified drawable or color resource to all system UI bars. 136 * 137 * @param res The identifier of the resource. 138*/139publicvoid setTintResource(int res) { 140 setStatusBarTintResource(res); 141 setNavigationBarTintResource(res); 142 } 143144/**145 * Apply the specified drawable to all system UI bars. 146 * 147 * @param drawable The drawable to use as the background, or null to remove it. 148*/149publicvoid setTintDrawable(Drawable drawable) { 150 setStatusBarTintDrawable(drawable); 151 setNavigationBarTintDrawable(drawable); 152 } 153154/**155 * Apply the specified alpha to all system UI bars. 156 * 157 * @param alpha The alpha to use 158*/159publicvoid setTintAlpha(float alpha) { 160 setStatusBarAlpha(alpha); 161 setNavigationBarAlpha(alpha); 162 } 163164/**165 * Apply the specified color tint to the system status bar. 166 * 167 * @param color The color of the background tint. 168*/169publicvoid setStatusBarTintColor(int color) { 170if (mStatusBarAvailable) { 171 mStatusBarTintView.setBackgroundColor(color); 172 } 173 } 174175/**176 * Apply the specified drawable or color resource to the system status bar. 177 * 178 * @param res The identifier of the resource. 179*/180publicvoid setStatusBarTintResource(int res) { 181if (mStatusBarAvailable) { 182 mStatusBarTintView.setBackgroundResource(res); 183 } 184 } 185186/**187 * Apply the specified drawable to the system status bar. 188 * 189 * @param drawable The drawable to use as the background, or null to remove it. 190*/191 @SuppressWarnings("deprecation") 192publicvoid setStatusBarTintDrawable(Drawable drawable) { 193if (mStatusBarAvailable) { 194 mStatusBarTintView.setBackgroundDrawable(drawable); 195 } 196 } 197198/**199 * Apply the specified alpha to the system status bar. 200 * 201 * @param alpha The alpha to use 202*/203 @TargetApi(11) 204publicvoid setStatusBarAlpha(float alpha) { 205if (mStatusBarAvailable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 206 mStatusBarTintView.setAlpha(alpha); 207 } 208 } 209210/**211 * Apply the specified color tint to the system navigation bar. 212 * 213 * @param color The color of the background tint. 214*/215publicvoid setNavigationBarTintColor(int color) { 216if (mNavBarAvailable) { 217 mNavBarTintView.setBackgroundColor(color); 218 } 219 } 220221/**222 * Apply the specified drawable or color resource to the system navigation bar. 223 * 224 * @param res The identifier of the resource. 225*/226publicvoid setNavigationBarTintResource(int res) { 227if (mNavBarAvailable) { 228 mNavBarTintView.setBackgroundResource(res); 229 } 230 } 231232/**233 * Apply the specified drawable to the system navigation bar. 234 * 235 * @param drawable The drawable to use as the background, or null to remove it. 236*/237 @SuppressWarnings("deprecation") 238publicvoid setNavigationBarTintDrawable(Drawable drawable) { 239if (mNavBarAvailable) { 240 mNavBarTintView.setBackgroundDrawable(drawable); 241 } 242 } 243244/**245 * Apply the specified alpha to the system navigation bar. 246 * 247 * @param alpha The alpha to use 248*/249 @TargetApi(11) 250publicvoid setNavigationBarAlpha(float alpha) { 251if (mNavBarAvailable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 252 mNavBarTintView.setAlpha(alpha); 253 } 254 } 255256/**257 * Get the system bar configuration. 258 * 259 * @return The system bar configuration for the current device configuration. 260*/261public SystemBarConfig getConfig() { 262return mConfig; 263 } 264265/**266 * Is tinting enabled for the system status bar? 267 * 268 * @return True if enabled, False otherwise. 269*/270publicboolean isStatusBarTintEnabled() { 271return mStatusBarTintEnabled; 272 } 273274/**275 * Is tinting enabled for the system navigation bar? 276 * 277 * @return True if enabled, False otherwise. 278*/279publicboolean isNavBarTintEnabled() { 280return mNavBarTintEnabled; 281 } 282283privatevoid setupStatusBarView(Context context, ViewGroup decorViewGroup) { 284 mStatusBarTintView = new View(context); 285 LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, mConfig.getStatusBarHeight()); 286 params.gravity = Gravity.TOP; 287if (mNavBarAvailable && !mConfig.isNavigationAtBottom()) { 288 params.rightMargin = mConfig.getNavigationBarWidth(); 289 } 290 mStatusBarTintView.setLayoutParams(params); 291 mStatusBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR); 292 mStatusBarTintView.setVisibility(View.GONE); 293 decorViewGroup.addView(mStatusBarTintView); 294 } 295296privatevoid setupNavBarView(Context context, ViewGroup decorViewGroup) { 297 mNavBarTintView = new View(context); 298 LayoutParams params; 299if (mConfig.isNavigationAtBottom()) { 300 params = new LayoutParams(LayoutParams.MATCH_PARENT, mConfig.getNavigationBarHeight()); 301 params.gravity = Gravity.BOTTOM; 302 } else { 303 params = new LayoutParams(mConfig.getNavigationBarWidth(), LayoutParams.MATCH_PARENT); 304 params.gravity = Gravity.RIGHT; 305 } 306 mNavBarTintView.setLayoutParams(params); 307 mNavBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR); 308 mNavBarTintView.setVisibility(View.GONE); 309 decorViewGroup.addView(mNavBarTintView); 310 } 311312/**313 * Class which describes system bar sizing and other characteristics for the current 314 * device configuration. 315 * 316*/317publicstaticclass SystemBarConfig { 318319privatestaticfinal String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height"; 320privatestaticfinal String NAV_BAR_HEIGHT_RES_NAME = "navigation_bar_height"; 321privatestaticfinal String NAV_BAR_HEIGHT_LANDSCAPE_RES_NAME = "navigation_bar_height_landscape"; 322privatestaticfinal String NAV_BAR_WIDTH_RES_NAME = "navigation_bar_width"; 323privatestaticfinal String SHOW_NAV_BAR_RES_NAME = "config_showNavigationBar"; 324325privatefinalboolean mTranslucentStatusBar; 326privatefinalboolean mTranslucentNavBar; 327privatefinalint mStatusBarHeight; 328privatefinalint mActionBarHeight; 329privatefinalboolean mHasNavigationBar; 330privatefinalint mNavigationBarHeight; 331privatefinalint mNavigationBarWidth; 332privatefinalboolean mInPortrait; 333privatefinalfloat mSmallestWidthDp; 334335private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) { 336 Resources res = activity.getResources(); 337 mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT); 338 mSmallestWidthDp = getSmallestWidthDp(activity); 339 mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME); 340 mActionBarHeight = getActionBarHeight(activity); 341 mNavigationBarHeight = getNavigationBarHeight(activity); 342 mNavigationBarWidth = getNavigationBarWidth(activity); 343 mHasNavigationBar = (mNavigationBarHeight > 0); 344 mTranslucentStatusBar = translucentStatusBar; 345 mTranslucentNavBar = traslucentNavBar; 346 } 347348 @TargetApi(14) 349privateint getActionBarHeight(Context context) { 350int result = 0; 351if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 352 TypedValue tv = new TypedValue(); 353 context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true); 354 result = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics()); 355 } 356return result; 357 } 358359 @TargetApi(14) 360privateint getNavigationBarHeight(Context context) { 361 Resources res = context.getResources(); 362int result = 0; 363if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 364if (hasNavBar(context)) { 365 String key; 366if (mInPortrait) { 367 key = NAV_BAR_HEIGHT_RES_NAME; 368 } else { 369 key = NAV_BAR_HEIGHT_LANDSCAPE_RES_NAME; 370 } 371return getInternalDimensionSize(res, key); 372 } 373 } 374return result; 375 } 376377 @TargetApi(14) 378privateint getNavigationBarWidth(Context context) { 379 Resources res = context.getResources(); 380int result = 0; 381if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 382if (hasNavBar(context)) { 383return getInternalDimensionSize(res, NAV_BAR_WIDTH_RES_NAME); 384 } 385 } 386return result; 387 } 388389 @TargetApi(14) 390privateboolean hasNavBar(Context context) { 391 Resources res = context.getResources(); 392int resourceId = res.getIdentifier(SHOW_NAV_BAR_RES_NAME, "bool", "android"); 393if (resourceId != 0) { 394boolean hasNav = res.getBoolean(resourceId); 395// check override flag (see static block)396if ("1".equals(sNavBarOverride)) { 397 hasNav = false; 398 } elseif ("0".equals(sNavBarOverride)) { 399 hasNav = true; 400 } 401return hasNav; 402 } else { // fallback403return !ViewConfiguration.get(context).hasPermanentMenuKey(); 404 } 405 } 406407privateint getInternalDimensionSize(Resources res, String key) { 408int result = 0; 409int resourceId = res.getIdentifier(key, "dimen", "android"); 410if (resourceId > 0) { 411 result = res.getDimensionPixelSize(resourceId); 412 } 413return result; 414 } 415416 @SuppressLint("NewApi") 417privatefloat getSmallestWidthDp(Activity activity) { 418 DisplayMetrics metrics = new DisplayMetrics(); 419if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 420 activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics); 421 } else { 422// TODO this is not correct, but we don‘t really care pre-kitkat423 activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); 424 } 425float widthDp = metrics.widthPixels / metrics.density; 426float heightDp = metrics.heightPixels / metrics.density; 427return Math.min(widthDp, heightDp); 428 } 429430/**431 * Should a navigation bar appear at the bottom of the screen in the current 432 * device configuration? A navigation bar may appear on the right side of 433 * the screen in certain configurations. 434 * 435 * @return True if navigation should appear at the bottom of the screen, False otherwise. 436*/437publicboolean isNavigationAtBottom() { 438return (mSmallestWidthDp >= 600 || mInPortrait); 439 } 440441/**442 * Get the height of the system status bar. 443 * 444 * @return The height of the status bar (in pixels). 445*/446publicint getStatusBarHeight() { 447return mStatusBarHeight; 448 } 449450/**451 * Get the height of the action bar. 452 * 453 * @return The height of the action bar (in pixels). 454*/455publicint getActionBarHeight() { 456return mActionBarHeight; 457 } 458459/**460 * Does this device have a system navigation bar? 461 * 462 * @return True if this device uses soft key navigation, False otherwise. 463*/464publicboolean hasNavigtionBar() { 465return mHasNavigationBar; 466 } 467468/**469 * Get the height of the system navigation bar. 470 * 471 * @return The height of the navigation bar (in pixels). If the device does not have 472 * soft navigation keys, this will always return 0. 473*/474publicint getNavigationBarHeight() { 475return mNavigationBarHeight; 476 } 477478/**479 * Get the width of the system navigation bar when it is placed vertically on the screen. 480 * 481 * @return The width of the navigation bar (in pixels). If the device does not have 482 * soft navigation keys, this will always return 0. 483*/484publicint getNavigationBarWidth() { 485return mNavigationBarWidth; 486 } 487488/**489 * Get the layout inset for any system UI that appears at the top of the screen. 490 * 491 * @param withActionBar True to include the height of the action bar, False otherwise. 492 * @return The layout inset (in pixels). 493*/494publicint getPixelInsetTop(boolean withActionBar) { 495return (mTranslucentStatusBar ? mStatusBarHeight : 0) + (withActionBar ? mActionBarHeight : 0); 496 } 497498/**499 * Get the layout inset for any system UI that appears at the bottom of the screen. 500 * 501 * @return The layout inset (in pixels). 502*/503publicint getPixelInsetBottom() { 504if (mTranslucentNavBar && isNavigationAtBottom()) { 505return mNavigationBarHeight; 506 } else { 507return 0; 508 } 509 } 510511/**512 * Get the layout inset for any system UI that appears at the right of the screen. 513 * 514 * @return The layout inset (in pixels). 515*/516publicint getPixelInsetRight() { 517if (mTranslucentNavBar && !isNavigationAtBottom()) { 518return mNavigationBarWidth; 519 } else { 520return 0; 521 } 522 } 523524 } 525526 }
---恢复内容结束---
原文:http://www.cnblogs.com/CharlesGrant/p/4944645.html
内容总结
以上是互联网集市为您收集整理的Android 沉浸模式全部内容,希望文章能够帮你解决Android 沉浸模式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】