### Java Full ImmersionBar Configuration Example Source: https://context7.com/gyf-dev/immersionbar/llms.txt This Java example demonstrates the complete API configuration for ImmersionBar, covering status bar, navigation bar, full-screen, keyboard, and listener setups. ```java public class FullConfigActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_full_config); Toolbar toolbar = findViewById(R.id.toolbar); ImmersionBar.with(this) // 状态栏配置 .statusBarColor(R.color.colorPrimary) // 状态栏颜色 .statusBarAlpha(0.0f) // 状态栏透明度 .statusBarDarkFont(true) // 状态栏深色字体 .statusBarColorTransform(R.color.colorAccent) // 状态栏变换颜色 // 导航栏配置 .navigationBarColor(R.color.colorPrimary) // 导航栏颜色 .navigationBarAlpha(0.0f) // 导航栏透明度 .navigationBarDarkIcon(true) // 导航栏深色图标 .navigationBarColorTransform(R.color.colorAccent) // 导航栏变换颜色 .navigationBarEnable(true) // 是否可修改导航栏 .navigationBarWithKitkatEnable(true) // 4.4 设备是否可修改导航栏 .navigationBarWithEMUI3Enable(true) // EMUI3.x 是否可修改导航栏 // 自动深色模式 .autoDarkModeEnable(true) // 自动切换深色/浅色模式 .autoStatusBarDarkModeEnable(true, 0.2f) // 自动状态栏字体变色 .autoNavigationBarDarkModeEnable(true, 0.2f) // 自动导航栏图标变色 // 全屏和隐藏配置 .fullScreen(false) // 全屏模式 .hideBar(BarHide.FLAG_SHOW_BAR) // 隐藏 Bar 配置 // 标题栏适配 .titleBar(toolbar) // 标题栏 .fitsSystemWindows(false) // fitsSystemWindows .fitsLayoutOverlapEnable(true) // 是否修复布局重叠 .supportActionBar(false) // 支持 ActionBar // View 颜色变换 .addViewSupportTransformColor(toolbar) // 添加颜色变换 View .viewAlpha(0.0f) // View 透明度 // 软键盘适配 .keyboardEnable(true) // 启用软键盘适配 .keyboardMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) .setOnKeyboardListener((isPopup, keyboardHeight) -> { // 软键盘监听 }) // 监听器 .setOnNavigationBarListener(show -> { // 导航栏显示隐藏监听 }) .setOnBarListener(barProperties -> { // Bar 状态监听 }) // 标签管理 .addTag("main") // 保存配置到标签 // 魅族 Flyme 特殊配置 .flymeOSStatusBarFontColor(R.color.black) // Flyme 状态栏字体颜色 // 应用配置 .init(); } @Override protected void onDestroy() { super.onDestroy(); // 无需手动销毁,ImmersionBar 会自动处理生命周期 } } ``` -------------------------------- ### Full Configuration Example Source: https://context7.com/gyf-dev/immersionbar/llms.txt Demonstrates a comprehensive example of ImmersionBar API configuration. ```APIDOC ## Full Configuration Example This example shows the complete API configuration for ImmersionBar. ```java public class FullConfigActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_full_config); Toolbar toolbar = findViewById(R.id.toolbar); ImmersionBar.with(this) // Status bar configuration .statusBarColor(R.color.colorPrimary) // Status bar color .statusBarAlpha(0.0f) // Status bar alpha .statusBarDarkFont(true) // Status bar dark font .statusBarColorTransform(R.color.colorAccent) // Status bar color transform // Navigation bar configuration .navigationBarColor(R.color.colorPrimary) // Navigation bar color .navigationBarAlpha(0.0f) // Navigation bar alpha .navigationBarDarkIcon(true) // Navigation bar dark icon .navigationBarColorTransform(R.color.colorAccent) // Navigation bar color transform .navigationBarEnable(true) // Enable navigation bar modification .navigationBarWithKitkatEnable(true) // Enable navigation bar modification on KitKat .navigationBarWithEMUI3Enable(true) // Enable navigation bar modification on EMUI 3.x // Auto dark mode .autoDarkModeEnable(true) // Enable auto dark/light mode switching .autoStatusBarDarkModeEnable(true, 0.2f) // Auto status bar font color change .autoNavigationBarDarkModeEnable(true, 0.2f) // Auto navigation bar icon color change // Full screen and hide configuration .fullScreen(false) // Full screen mode .hideBar(BarHide.FLAG_SHOW_BAR) // Hide bar configuration // Title bar adaptation .titleBar(toolbar) // Title bar .fitsSystemWindows(false) // fitsSystemWindows .fitsLayoutOverlapEnable(true) // Fix layout overlap .supportActionBar(false) // Support ActionBar // View color transformation .addViewSupportTransformColor(toolbar) // Add view for color transformation .viewAlpha(0.0f) // View alpha // Soft keyboard adaptation .keyboardEnable(true) // Enable soft keyboard adaptation .keyboardMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) .setOnKeyboardListener((isPopup, keyboardHeight) -> { // Soft keyboard listener }) // Listener .setOnNavigationBarListener(show -> { // Navigation bar show/hide listener }) .setOnBarListener(barProperties -> { // Bar state listener }) // Tag management .addTag("main") // Save configuration to tag // Meizu Flyme specific configuration .flymeOSStatusBarFontColor(R.color.black) // Flyme status bar font color // Apply configuration .init(); } @Override protected void onDestroy() { super.onDestroy(); // No manual destruction needed, ImmersionBar handles lifecycle automatically } } ``` ``` -------------------------------- ### Get System Bar Dimensions Source: https://context7.com/gyf-dev/immersionbar/llms.txt Utilize static methods to retrieve dimensions like status bar height, navigation bar height and width, and action bar height. ```java int statusBarHeight = ImmersionBar.getStatusBarHeight(this); ``` ```java int navBarHeight = ImmersionBar.getNavigationBarHeight(this); ``` ```java int navBarWidth = ImmersionBar.getNavigationBarWidth(this); ``` ```java int actionBarHeight = ImmersionBar.getActionBarHeight(this); ``` -------------------------------- ### Initialize ImmersionBar in Activity (Kotlin) Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md Kotlin syntax for initializing ImmersionBar within an Activity. Provides a more concise way to apply basic immersive settings. ```kotlin immersionBar { statusBarColor(R.color.colorPrimary) navigationBarColor(R.color.colorPrimary) } ``` -------------------------------- ### Set fitsSystemWindows with Static Method Source: https://context7.com/gyf-dev/immersionbar/llms.txt The fitsSystemWindows property can be set using a static method. ```java ImmersionBar.setFitsSystemWindows(this, true); ``` -------------------------------- ### Kotlin ImmersionBar Initialization Source: https://github.com/gyf-dev/immersionbar/wiki/questions Use this extension function for a more concise way to initialize ImmersionBar in Kotlin, similar to the Java `ImmersionBar.with(this).init();`. ```kotlin immersionBar() ``` -------------------------------- ### Enable Resizeable Activity Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md Configure your AndroidManifest.xml to enable resizeable activity for full-screen support. This is often unnecessary if targetSdkVersion is 25 or higher. ```xml android:resizeableActivity="true" ``` -------------------------------- ### Manage Parameter Tags Source: https://context7.com/gyf-dev/immersionbar/llms.txt Use `addTag` to save current immersion configurations and `getTag` to restore them. This is useful for managing different page states. ```java ImmersionBar.with(this) .statusBarColor(R.color.colorPrimary) .statusBarDarkFont(true) .addTag("home") .init(); ``` ```java ImmersionBar.with(this) .statusBarColor(R.color.white) .statusBarDarkFont(false) .addTag("detail") .init(); ``` ```java ImmersionBar.with(this) .getTag("home") .init(); ``` -------------------------------- ### Advanced ImmersionBar Configuration Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md Demonstrates extensive configuration options for ImmersionBar, including status bar and navigation bar transparency, colors, alpha, font/icon dark mode, full-screen behavior, bar hiding, view transformations, and keyboard handling. Use this for fine-grained control over system bar appearance and behavior. ```java ImmersionBar.with(this) .transparentStatusBar() //透明状态栏,不写默认透明色 .transparentNavigationBar() //透明导航栏,不写默认黑色(设置此方法,fullScreen()方法自动为true) .transparentBar() //透明状态栏和导航栏,不写默认状态栏为透明色,导航栏为黑色(设置此方法,fullScreen()方法自动为true) .statusBarColor(R.color.colorPrimary) //状态栏颜色,不写默认透明色 .navigationBarColor(R.color.colorPrimary) //导航栏颜色,不写默认黑色 .barColor(R.color.colorPrimary) //同时自定义状态栏和导航栏颜色,不写默认状态栏为透明色,导航栏为黑色 .statusBarAlpha(0.3f) //状态栏透明度,不写默认0.0f .navigationBarAlpha(0.4f) //导航栏透明度,不写默认0.0F .barAlpha(0.3f) //状态栏和导航栏透明度,不写默认0.0f .statusBarDarkFont(true) //状态栏字体是深色,不写默认为亮色 .navigationBarDarkIcon(true) //导航栏图标是深色,不写默认为亮色 .autoDarkModeEnable(true) //自动状态栏字体和导航栏图标变色,必须指定状态栏颜色和导航栏颜色才可以自动变色哦 .autoStatusBarDarkModeEnable(true,0.2f) //自动状态栏字体变色,必须指定状态栏颜色才可以自动变色哦 .autoNavigationBarDarkModeEnable(true,0.2f) //自动导航栏图标变色,必须指定导航栏颜色才可以自动变色哦 .flymeOSStatusBarFontColor(R.color.btn3) //修改flyme OS状态栏字体颜色 .fullScreen(true) //有导航栏的情况下,activity全屏显示,也就是activity最下面被导航栏覆盖,不写默认非全屏 .hideBar(BarHide.FLAG_HIDE_BAR) //隐藏状态栏或导航栏或两者,不写默认不隐藏 .addViewSupportTransformColor(toolbar) //设置支持view变色,可以添加多个view,不指定颜色,默认和状态栏同色,还有两个重载方法 .titleBar(view) //解决状态栏和布局重叠问题,任选其一 .titleBarMarginTop(view) //解决状态栏和布局重叠问题,任选其一 .statusBarView(view) //解决状态栏和布局重叠问题,任选其一 .fitsSystemWindows(true) //解决状态栏和布局重叠问题,任选其一,默认为false,当为true时一定要指定statusBarColor(),不然状态栏为透明色,还有一些重载方法 .supportActionBar(true) //支持ActionBar使用 .statusBarColorTransform(R.color.orange) //状态栏变色后的颜色 .navigationBarColorTransform(R.color.orange) //导航栏变色后的颜色 .barColorTransform(R.color.orange) //状态栏和导航栏变色后的颜色 .removeSupportView(toolbar) //移除指定view支持 .removeSupportAllView() //移除全部view支持 .navigationBarEnable(true) //是否可以修改导航栏颜色,默认为true .navigationBarWithKitkatEnable(true) //是否可以修改安卓4.4和emui3.x手机导航栏颜色,默认为true .navigationBarWithEMUI3Enable(true) //是否可以修改emui3.x手机导航栏颜色,默认为true .keyboardEnable(true) //解决软键盘与底部输入框冲突问题,默认为false,还有一个重载方法,可以指定软键盘mode .keyboardMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) //单独指定软键盘模式 .setOnKeyboardListener(new OnKeyboardListener() { //软键盘监听回调,keyboardEnable为true才会回调此方法 @Override public void onKeyboardChange(boolean isPopup, int keyboardHeight) { LogUtils.e(isPopup); //isPopup为true,软键盘弹出,为false,软键盘关闭 } }) .setOnNavigationBarListener(onNavigationBarListener) //导航栏显示隐藏监听,目前只支持华为和小米手机 .setOnBarListener(OnBarListener) //第一次调用和横竖屏切换都会触发,可以用来做刘海屏遮挡布局控件的问题 .addTag("tag") //给以上设置的参数打标记 .getTag("tag") //根据tag获得沉浸式参数 .reset() //重置所以沉浸式参数 .init(); //必须调用方可应用以上所配置的参数 ``` -------------------------------- ### Listen for Keyboard Changes with OnKeyboardListener Source: https://context7.com/gyf-dev/immersionbar/llms.txt Implement the OnKeyboardListener interface to receive callbacks when the soft keyboard appears or disappears, providing the keyboard height. ```java ImmersionBar.with(this) .keyboardEnable(true) .setOnKeyboardListener(new OnKeyboardListener() { @Override public void onKeyboardChange(boolean isPopup, int keyboardHeight) { if (isPopup) { // 软键盘弹出,keyboardHeight 为键盘高度 } else { // 软键盘关闭 } } }) .init(); ``` -------------------------------- ### Enable Keyboard Adaptation with keyboardEnable() Source: https://context7.com/gyf-dev/immersionbar/llms.txt Resolve conflicts between the soft keyboard and bottom input fields by enabling keyboard adaptation. Specific soft input modes can be specified, including auto-popup behavior. ```java ImmersionBar.with(this) .keyboardEnable(true) .init(); ``` ```java ImmersionBar.with(this) .keyboardEnable(true, WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) .init(); ``` ```java ImmersionBar.with(this) .keyboardEnable(true, WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) .init(); ``` -------------------------------- ### Check System Bar Presence and Properties Source: https://context7.com/gyf-dev/immersionbar/llms.txt Use static utility methods to determine if a navigation bar exists, if it's positioned at the bottom, or if the device has a notch screen. ```java boolean hasNavBar = ImmersionBar.hasNavigationBar(this); ``` ```java boolean isAtBottom = ImmersionBar.isNavigationAtBottom(this); ``` ```java boolean hasNotch = ImmersionBar.hasNotchScreen(this); ``` ```java int notchHeight = ImmersionBar.getNotchHeight(this); ``` -------------------------------- ### Enable Full Screen Mode with fullScreen() Source: https://context7.com/gyf-dev/immersionbar/llms.txt Enable full-screen mode, allowing the bottom layout to extend below the navigation bar. This can be combined with transparent navigation bars. ```java ImmersionBar.with(this) .fullScreen(true) .init(); ``` ```java ImmersionBar.with(this) .transparentNavigationBar() // 自动开启 fullScreen .init(); ``` ```java ImmersionBar.with(this) .fullScreen(true) .navigationBarColor(R.color.colorPrimary) .navigationBarAlpha(0.5f) .init(); ``` -------------------------------- ### Initialize ImmersionBar in Activity Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md Basic initialization of ImmersionBar within an Activity. Call this method to apply default immersive settings. ```java ImmersionBar.with(this).init(); ``` -------------------------------- ### ImmersionBar fitsSystemWindows Method Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md Apply ImmersionBar's `fitsSystemWindows(true)` method for pure color status bars. This requires setting the `statusBarColor`. ```java ImmersionBar.with(this) .fitsSystemWindows(true) //使用该属性,必须指定状态栏颜色 .statusBarColor(R.color.colorPrimary) .init(); ``` -------------------------------- ### Status Bar and Navigation Bar Utilities Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md This section details utility methods for checking the presence, dimensions, and properties of the navigation bar and status bar, as well as methods for controlling their visibility. ```APIDOC ## Utility Methods for Status Bar and Navigation Bar ### Description Provides utility methods to query and manipulate the status bar and navigation bar. ### Methods #### `hasNavigationBar(Activity activity)` - **Description**: Checks if the device has a navigation bar. - **Parameters**: - `activity` (Activity) - The activity context. - **Returns**: `boolean` - True if a navigation bar exists, false otherwise. #### `getNavigationBarHeight(Activity activity)` - **Description**: Gets the height of the navigation bar. - **Parameters**: - `activity` (Activity) - The activity context. - **Returns**: `int` - The height of the navigation bar in pixels. #### `getNavigationBarWidth(Activity activity)` - **Description**: Gets the width of the navigation bar. - **Parameters**: - `activity` (Activity) - The activity context. - **Returns**: `int` - The width of the navigation bar in pixels. #### `isNavigationAtBottom(Activity activity)` - **Description**: Checks if the navigation bar is located at the bottom of the screen. - **Parameters**: - `activity` (Activity) - The activity context. - **Returns**: `boolean` - True if the navigation bar is at the bottom, false otherwise. #### `getStatusBarHeight(Activity activity)` - **Description**: Gets the height of the status bar. - **Parameters**: - `activity` (Activity) - The activity context. - **Returns**: `int` - The height of the status bar in pixels. #### `getActionBarHeight(Activity activity)` - **Description**: Gets the height of the ActionBar. - **Parameters**: - `activity` (Activity) - The activity context. - **Returns**: `int` - The height of the ActionBar in pixels. #### `hasNotchScreen(Activity activity)` - **Description**: Checks if the device has a notch screen. - **Parameters**: - `activity` (Activity) - The activity context. - **Returns**: `boolean` - True if the device has a notch screen, false otherwise. #### `getNotchHeight(Activity activity)` - **Description**: Gets the height of the notch screen. - **Parameters**: - `activity` (Activity) - The activity context. - **Returns**: `boolean` - The height of the notch screen. Note: The return type is specified as boolean in the source, which might be a typo and should ideally be an int. #### `isSupportStatusBarDarkFont()` - **Description**: Checks if the current device supports dark font for the status bar. - **Returns**: `boolean` - True if dark font is supported, false otherwise. #### `isSupportNavigationIconDark()` - **Description**: Checks if the current device supports dark icons for the navigation bar. - **Returns**: `boolean` - True if dark icons are supported, false otherwise. #### `hideStatusBar(Window window)` - **Description**: Hides the status bar. - **Parameters**: - `window` (Window) - The window object. - **Returns**: `void` ``` -------------------------------- ### Adapt to System Windows with fitsSystemWindows() Source: https://context7.com/gyf-dev/immersionbar/llms.txt Utilize the system's fitsSystemWindows property to handle status bar overlap, primarily for pure color status bars. This method can also be configured for swipe-back gestures and transparency. ```java ImmersionBar.with(this) .fitsSystemWindows(true) .statusBarColor(R.color.colorPrimary) .init(); ``` ```java ImmersionBar.with(this) .fitsSystemWindows(true, R.color.white) // 设置整体背景色 .statusBarColor(R.color.colorPrimary) .init(); ``` ```java ImmersionBar.with(this) .fitsSystemWindowsInt(true, Color.WHITE, Color.BLACK, 0.5f) .statusBarColor(R.color.colorPrimary) .init(); ``` -------------------------------- ### Check View Fits System Windows Source: https://context7.com/gyf-dev/immersionbar/llms.txt Verify if a given View is configured to use `fitsSystemWindows` attribute. ```java boolean hasFits = ImmersionBar.checkFitsSystemWindows(view); ``` -------------------------------- ### Check Feature Support and Gestures Source: https://context7.com/gyf-dev/immersionbar/llms.txt Determine if the system supports dark status bar fonts, dark navigation bar icons, or if gesture navigation is enabled. ```java boolean supportDarkFont = ImmersionBar.isSupportStatusBarDarkFont(); ``` ```java boolean supportDarkIcon = ImmersionBar.isSupportNavigationIconDark(); ``` ```java boolean isGesture = ImmersionBar.isGesture(this); ``` -------------------------------- ### Initialize ImmersionBar in Dialog Source: https://context7.com/gyf-dev/immersionbar/llms.txt Initialize and destroy ImmersionBar instances for Dialogs, passing the Dialog context. ```java // 在 Dialog 中使用 Dialog dialog = new Dialog(this); // dialog.setContentView(R.layout.dialog_layout); // ImmersionBar.with(this, dialog).init(); // 关闭 Dialog 时销毁 // ImmersionBar.destroy(this, dialog); ``` -------------------------------- ### Conveniently Use ActionBar Title Source: https://github.com/gyf-dev/immersionbar/wiki/home Use the supportActionBar() method for easier access to the ActionBar's title bar. ```java supportActionBar() ``` -------------------------------- ### Use fitsSystemWindows for Status Bar Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md Utilize the system's `fitsSystemWindows` attribute on the root layout for pure color status bars. When using ImmersionBar, ensure `statusBarColor` is set. ```xml ``` ```java ImmersionBar.with(this) .statusBarColor(R.color.colorPrimary) .init(); ``` -------------------------------- ### Enable Soft Keyboard Handling with ImmersionBar Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md Enable soft keyboard handling using `keyboardEnable(true)` to resolve conflicts between the keyboard and bottom input fields. An optional second parameter can control automatic keyboard popping. ```java ImmersionBar.with(this) .keyboardEnable(true) //解决软键盘与底部输入框冲突问题 // .keyboardEnable(true, WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE // | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) //软键盘自动弹出 .init(); ``` -------------------------------- ### Enable Immersive Mode with fitsSystemWindows(true) Source: https://github.com/gyf-dev/immersionbar/wiki/home To fix status bar and layout overlap issues, call fitsSystemWindows(true) and specify the status bar color using statusBarColor(). ```java fitsSystemWindows(true) ``` ```java statusBarColor() ``` -------------------------------- ### Make Status and Navigation Bars Transparent Source: https://context7.com/gyf-dev/immersionbar/llms.txt Set both the status bar and navigation bar to be completely transparent. ```java // 同时透明状态栏和导航栏 ImmersionBar.with(this) .transparentBar() .init(); ``` -------------------------------- ### Set Status Bar Color using Resources Source: https://context7.com/gyf-dev/immersionbar/llms.txt Set the status bar background color using a color resource ID. ```java // 使用资源文件设置颜色 ImmersionBar.with(this) .statusBarColor(R.color.colorPrimary) .init(); ``` -------------------------------- ### Handle ViewPager Page Changes with Tags Source: https://context7.com/gyf-dev/immersionbar/llms.txt Integrate `getTag` with `ViewPager.OnPageChangeListener` to apply saved immersion configurations when pages change. ```java viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int position) { switch (position) { case 0: ImmersionBar.with(MainActivity.this).getTag("home").init(); break; case 1: ImmersionBar.with(MainActivity.this).getTag("detail").init(); break; } } // ... 其他方法省略 }); ``` -------------------------------- ### Proguard Rules Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md Configuration for Proguard/R8 to ensure the ImmersionBar library functions correctly when code obfuscation is enabled. ```APIDOC ## Proguard Rules ### Description Provides Proguard rules for the ImmersionBar library. ### Versions - **3.1.1 and above**: No obfuscation required. - **Below 3.0.0**: Use the following rules: ```proguard -keep class com.gyf.immersionbar.* -dontwarn com.gyf.immersionbar.** ``` ``` -------------------------------- ### Add ImmersionBar Dependency Source: https://context7.com/gyf-dev/immersionbar/llms.txt Add the ImmersionBar library and its Kotlin extensions to your module's build.gradle file. ```groovy // build.gradle (Module) dependencies { // 基础依赖包,必须依赖 implementation 'com.geyifeng.immersionbar:immersionbar:3.2.2' // Kotlin 扩展(可选) implementation 'com.geyifeng.immersionbar:immersionbar-ktx:3.2.2' } ``` -------------------------------- ### Initialize ImmersionBar in Other Dialogs Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md Initialize ImmersionBar for other types of dialogs. Remember to call the destroy method when the dialog is closed to prevent memory leaks. ```java ImmersionBar.with(this, dialog).init(); ``` -------------------------------- ### Initialize ImmersionBar in Fragment Source: https://context7.com/gyf-dev/immersionbar/llms.txt Call ImmersionBar.with(this).init() within the onViewCreated method of a Fragment. ```java // 在 Fragment 中使用 public class HomeFragment extends Fragment { @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ImmersionBar.with(this).init(); } } ``` -------------------------------- ### ImmersionBar Dependencies (3.1.1+) Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md Add these dependencies to your project for ImmersionBar. The kotlin extension and fragment component are optional. ```groovy implementation 'com.geyifeng.immersionbar:immersionbar:3.2.2' //kotlin扩展(可选) implementation 'com.geyifeng.immersionbar:immersionbar-ktx:3.2.2' // fragment快速实现(可选)已废弃 implementation 'com.geyifeng.immersionbar:immersionbar-components:3.2.2' ``` -------------------------------- ### Set Status Bar Placeholder View with statusBarView() Source: https://context7.com/gyf-dev/immersionbar/llms.txt Use the statusBarView() method to set a placeholder View for the status bar, accepting either a View object or a resource ID. Static methods are also available. ```java ImmersionBar.with(this) .statusBarView(statusBarView) .init(); ``` ```java ImmersionBar.with(this) .statusBarView(R.id.status_bar_view) .init(); ``` ```java ImmersionBar.setStatusBarView(this, statusBarView); ``` -------------------------------- ### Make Status Bar Transparent Source: https://context7.com/gyf-dev/immersionbar/llms.txt Set the status bar to be completely transparent. ```java // 透明状态栏 ImmersionBar.with(this) .transparentStatusBar() .init(); ``` -------------------------------- ### Create Status Bar Placeholder View Source: https://context7.com/gyf-dev/immersionbar/llms.txt Add a View with a height equal to the status bar height to resolve overlap issues, suitable for gradient status bars and swipe-back gestures. The View's initial height should be 0dp. ```xml ``` -------------------------------- ### Set Bar State Change Listener Source: https://context7.com/gyf-dev/immersionbar/llms.txt Use `setOnBarListener` to monitor changes in Bar properties, such as orientation. This can be useful for handling notch screen obstructions. ```java ImmersionBar.with(this) .setOnBarListener(new OnBarListener() { @Override public void onBarChange(BarProperties barProperties) { // 可用于处理刘海屏遮挡问题 if (barProperties.isPortrait()) { // 竖屏 } else { // 横屏 } } }) .init(); ``` -------------------------------- ### Set Status Bar Color with Different Formats Source: https://github.com/gyf-dev/immersionbar/wiki/home Provides overloaded methods for setting the status bar color using resource IDs, hex strings, or integer color values for convenience. ```java statusBarColor(R.color.white) ``` ```java statusBarColor("#ffffff") ``` ```java statusBarColorInt(0xffffffff) ``` -------------------------------- ### Set Navigation Bar Dark Icon with Fallback Alpha Source: https://context7.com/gyf-dev/immersionbar/llms.txt Set dark navigation bar icons and apply a fallback alpha transparency for unsupported devices. ```java // 设置深色图标,并为不支持的设备添加透明度兜底 ImmersionBar.with(this) .navigationBarColor(R.color.white) .navigationBarDarkIcon(true, 0.2f) .init(); ``` -------------------------------- ### Kotlin ImmersionBar with Status Bar Color Source: https://github.com/gyf-dev/immersionbar/wiki/questions This Kotlin extension provides a fluent API for setting the status bar color during ImmersionBar initialization, equivalent to Java's `ImmersionBar.with(this).statusBarColor(R.color.colorPrimary).init();`. ```kotlin immersionBar { statusBarColor(R.color.colorPrimary) } ``` -------------------------------- ### Make Navigation Bar Transparent Source: https://context7.com/gyf-dev/immersionbar/llms.txt Set the navigation bar to be completely transparent, which automatically enables full-screen mode. ```java // 透明导航栏(自动开启全屏模式) ImmersionBar.with(this) .transparentNavigationBar() .init(); ``` -------------------------------- ### Hide/Show Status Bar with Static Method Source: https://context7.com/gyf-dev/immersionbar/llms.txt Static methods are available to hide or show the status bar. ```java ImmersionBar.hideStatusBar(getWindow()); ImmersionBar.showStatusBar(getWindow()); ``` -------------------------------- ### Kotlin Extension Functions Source: https://context7.com/gyf-dev/immersionbar/llms.txt Simplify ImmersionBar calls using Kotlin extension functions and properties for various scenarios like Activities, Fragments, and Dialogs. ```APIDOC ## Kotlin Extension Functions Use Kotlin extension functions to simplify ImmersionBar calls. ### Basic Usage in Activity ```kotlin class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) immersionBar { statusBarColor(R.color.colorPrimary) navigationBarColor(R.color.colorPrimary) statusBarDarkFont(true) } } } ``` ### Usage in Fragment ```kotlin class HomeFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) immersionBar { statusBarColor(R.color.white) statusBarDarkFont(true) titleBar(view.findViewById(R.id.toolbar)) } } } ``` ### Usage in Dialog ```kotlin immersionBar(dialog) { statusBarColor(R.color.colorPrimary) } // Destroying Dialog destroyImmersionBar(dialog) ``` ### Extension Properties for System Bar Info - `statusBarHeight`: Status bar height - `navigationBarHeight`: Navigation bar height - `navigationBarWidth`: Navigation bar width - `actionBarHeight`: ActionBar height - `hasNavigationBar`: Whether the navigation bar exists - `hasNotchScreen`: Whether it's a notch screen - `notchHeight`: Notch screen height - `isNavigationAtBottom`: Whether the navigation bar is at the bottom - `isGesture`: Whether gesture navigation is used ### Extension Functions - `fitsTitleBar(toolbar)`: Set title bar - `fitsTitleBarMarginTop(toolbar)`: Set title bar marginTop - `fitsStatusBarView(statusBarView)`: Set status bar placeholder View - `hideStatusBar()`: Hide status bar - `showStatusBar()`: Show status bar - `setFitsSystemWindows()`: Set fitsSystemWindows ``` -------------------------------- ### Enable Immersion on Android 4.4 without Navigation Bar Source: https://github.com/gyf-dev/immersionbar/wiki/home Addresses a bug where fitsSystemWindows(true) does not work on Android 4.4 devices without a navigation bar. ```java fitsSystemWindows(true) ``` -------------------------------- ### Set Navigation Bar Visibility Listener Source: https://context7.com/gyf-dev/immersionbar/llms.txt Implement `OnNavigationBarListener` to receive callbacks when the navigation bar's visibility changes. This is supported on Huawei and Xiaomi devices. ```java ImmersionBar.with(this) .setOnNavigationBarListener(new OnNavigationBarListener() { @Override public void onNavigationBarChange(boolean show) { if (show) { // 导航栏显示 } else { // 导航栏隐藏 } } }) .init(); ``` -------------------------------- ### Set Status Bar Color Gradient Source: https://context7.com/gyf-dev/immersionbar/llms.txt Create a color gradient for the status bar, useful for scroll effects. ```java // 设置颜色渐变(用于滚动时颜色变化) ImmersionBar.with(this) .statusBarColor(R.color.colorPrimary, R.color.colorAccent, 0.5f) .init(); ``` -------------------------------- ### Set Status Bar Dark Font with Fallback Alpha Source: https://context7.com/gyf-dev/immersionbar/llms.txt Set dark status bar font and apply a fallback alpha transparency for devices that don't support dark font. ```java // 设置深色字体,并为不支持的设备添加透明度兜底 ImmersionBar.with(this) .statusBarColor(R.color.white) .statusBarDarkFont(true, 0.2f) // 不支持时添加 20% 透明度 .init(); ``` -------------------------------- ### ImmersionBar Dependencies (3.0.0) Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md Dependencies for ImmersionBar version 3.0.0. The kotlin extension and fragment component are optional. ```groovy implementation 'com.gyf.immersionbar:immersionbar:3.0.0' //kotlin扩展(可选) implementation 'com.gyf.immersionbar:immersionbar-ktx:3.0.0' // fragment快速实现(可选) implementation 'com.gyf.immersionbar:immersionbar-components:3.0.0' ``` -------------------------------- ### Remove SystemBarTint Dependency Source: https://github.com/gyf-dev/immersionbar/wiki/home The library no longer depends on the SystemBarTint library, indicating a move to a self-contained solution. ```java SystemBarTint ``` -------------------------------- ### Handle Navigation Bar Blank Space on Android 4.4 Source: https://github.com/gyf-dev/immersionbar/wiki/home Fixes an issue where a blank space appears at the bottom of the screen on Android 4.4 devices with a navigation bar when using android:fitsSystemWindows="true". ```xml android:fitsSystemWindows="true" ``` -------------------------------- ### Set Max Aspect Ratio Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md Configure your AndroidManifest.xml to set the maximum aspect ratio for your application. This is an alternative method for full-screen support. ```xml android:maxAspectRatio="2.4" ``` -------------------------------- ### Set Keyboard Mode with keyboardMode() Source: https://context7.com/gyf-dev/immersionbar/llms.txt Independently set the soft keyboard mode using the keyboardMode() method. ```java ImmersionBar.with(this) .keyboardMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN) .init(); ``` -------------------------------- ### Introduce KeyboardPatch for Soft Keyboard Issues Source: https://github.com/gyf-dev/immersionbar/wiki/home The KeyboardPatch class is introduced to resolve issues related to the soft keyboard and EditText at the bottom of the screen. ```java KeyboardPatch ``` -------------------------------- ### ImmersionBar statusBarView for Gradient Status Bars Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md Use ImmersionBar's `statusBarView(View view)` method to adapt gradient status bars or handle swipe-to-go-back gestures. Add a View with 0dp height above the toolbar. ```xml ``` ```java ImmersionBar.with(this) .statusBarView(view) .init(); //或者 //ImmersionBar.setStatusBarView(this,view); ``` -------------------------------- ### Initialize ImmersionBar in DialogFragment Source: https://context7.com/gyf-dev/immersionbar/llms.txt Initialize ImmersionBar within the onViewCreated method of a DialogFragment. ```java // 在 DialogFragment 中使用 public class MyDialogFragment extends DialogFragment { @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ImmersionBar.with(this).init(); } } ``` -------------------------------- ### Set Status and Navigation Bar Color Simultaneously Source: https://context7.com/gyf-dev/immersionbar/llms.txt Use barColor() to set both the status bar and navigation bar colors with a single call. ```java // 同时设置状态栏和导航栏颜色 ImmersionBar.with(this) .barColor(R.color.colorPrimary) .init(); ``` -------------------------------- ### Set Max Aspect Ratio Meta-data Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md Use a meta-data tag in your AndroidManifest.xml to specify the maximum aspect ratio. This is another approach for full-screen compatibility. ```xml ``` -------------------------------- ### Kotlin Extension Functions for ImmersionBar Source: https://context7.com/gyf-dev/immersionbar/llms.txt Use Kotlin extension functions to simplify ImmersionBar calls in Activities, Fragments, and Dialogs. Extension properties can also retrieve system bar information. ```kotlin class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // 简洁的 DSL 写法 immersionBar { statusBarColor(R.color.colorPrimary) navigationBarColor(R.color.colorPrimary) statusBarDarkFont(true) } } } // Fragment 中使用 class HomeFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) immersionBar { statusBarColor(R.color.white) statusBarDarkFont(true) titleBar(view.findViewById(R.id.toolbar)) } } } // Dialog 中使用 immersionBar(dialog) { statusBarColor(R.color.colorPrimary) } // 销毁 Dialog 时 destroyImmersionBar(dialog) // 扩展属性获取系统栏信息 val statusHeight = statusBarHeight // 状态栏高度 val navHeight = navigationBarHeight // 导航栏高度 val navWidth = navigationBarWidth // 导航栏宽度 val actionHeight = actionBarHeight // ActionBar 高度 val hasNav = hasNavigationBar // 是否有导航栏 val hasNotch = hasNotchScreen // 是否是刘海屏 val notch = notchHeight // 刘海屏高度 val isBottom = isNavigationAtBottom // 导航栏是否在底部 val gesture = isGesture // 是否使用手势导航 // 扩展函数 fitsTitleBar(toolbar) // 设置 titleBar fitsTitleBarMarginTop(toolbar) // 设置 titleBar marginTop fitsStatusBarView(statusBarView) // 设置状态栏占位 View hideStatusBar() // 隐藏状态栏 showStatusBar() // 显示状态栏 setFitsSystemWindows() // 设置 fitsSystemWindows ``` -------------------------------- ### ImmersionBar ProGuard Rules Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md Use these ProGuard rules for versions below 3.0.0 to prevent issues with code obfuscation. Versions 3.1.1 and above do not require these rules. ```proguard -keep class com.gyf.immersionbar.* {*;} -dontwarn com.gyf.immersionbar.** ``` -------------------------------- ### Set Title Bar with Static Method Source: https://context7.com/gyf-dev/immersionbar/llms.txt Static methods can be used to set the title bar or its top margin without needing an ImmersionBar instance. ```java ImmersionBar.setTitleBar(this, toolbar); ImmersionBar.setTitleBarMarginTop(this, toolbar); ``` -------------------------------- ### Set Status and Navigation Bar Alpha Simultaneously Source: https://context7.com/gyf-dev/immersionbar/llms.txt Set a uniform alpha transparency value for both the status bar and navigation bar. ```java // 或同时设置状态栏和导航栏透明度 ImmersionBar.with(this) .barAlpha(0.3f) .init(); ``` -------------------------------- ### Set Navigation Bar Color with Alpha Source: https://context7.com/gyf-dev/immersionbar/llms.txt Set the navigation bar color with a specified alpha transparency. ```java // 设置导航栏颜色和透明度 ImmersionBar.with(this) .navigationBarColor(R.color.colorPrimary, 0.5f) .init(); ``` -------------------------------- ### Hide Status Bar or Navigation Bar with hideBar() Source: https://context7.com/gyf-dev/immersionbar/llms.txt Hide the status bar, navigation bar, or both to achieve a full-screen immersive experience. Bars can also be explicitly shown. ```java ImmersionBar.with(this) .hideBar(BarHide.FLAG_HIDE_STATUS_BAR) .init(); ``` ```java ImmersionBar.with(this) .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR) .init(); ``` ```java ImmersionBar.with(this) .hideBar(BarHide.FLAG_HIDE_BAR) .init(); ``` ```java ImmersionBar.with(this) .hideBar(BarHide.FLAG_SHOW_BAR) .init(); ``` -------------------------------- ### Enable Huawei Notch Screen Support Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md Add this meta-data tag to your AndroidManifest.xml to enable compatibility with Huawei notch screens. ```xml ``` -------------------------------- ### Remove View Color Transformation Support Source: https://context7.com/gyf-dev/immersionbar/llms.txt Use `removeSupportView` to remove color transformation support for a specific View, or `removeSupportAllView` to remove it for all Views. ```java ImmersionBar.with(this) .removeSupportView(toolbar) .init(); ``` ```java ImmersionBar.with(this) .removeSupportAllView() .init(); ``` -------------------------------- ### Reset ImmersionBar Parameters Source: https://github.com/gyf-dev/immersionbar/wiki/home The reset() method provides a one-click solution to revert all ImmersionBar parameters to their default settings. ```java reset() ``` -------------------------------- ### Enable Auto Dark Mode for Status Bar with Threshold Source: https://context7.com/gyf-dev/immersionbar/llms.txt Automatically switch status bar font color with a specified transparency threshold for dark mode. ```java // 自动模式并设置透明度阈值 ImmersionBar.with(this) .statusBarColor(R.color.colorPrimary) .autoStatusBarDarkModeEnable(true, 0.2f) .init(); ``` -------------------------------- ### Retrieve Bar Properties by Tag Source: https://github.com/gyf-dev/immersionbar/wiki/home The getTag() method allows you to restore ImmersionBar properties to a previous state based on a previously set tag. ```java getTag() ``` -------------------------------- ### Enable Xiaomi Notch Screen Support Source: https://github.com/gyf-dev/immersionbar/blob/master/README.md Configure your AndroidManifest.xml with this meta-data tag for notch screen support on Xiaomi devices, supporting both portrait and landscape modes. ```xml ``` -------------------------------- ### Set Status Bar Color using ColorInt Source: https://context7.com/gyf-dev/immersionbar/llms.txt Set the status bar color directly using a ColorInt value. ```java // 使用 ColorInt 直接设置颜色值 ImmersionBar.with(this) .statusBarColorInt(Color.parseColor("#FF5722")) .init(); ``` -------------------------------- ### Enable View Color Change Feature Source: https://github.com/gyf-dev/immersionbar/wiki/home This feature allows specified Views to change color, likely in conjunction with status bar or navigation bar changes. ```java View支持变色功能 ```