Can I set FLAG_LAYOUT_NO_LIMITS only for status bar?

前端 未结 13 1571
旧时难觅i
旧时难觅i 2020-12-23 13:32

I need to make transparent status bar. I am using getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS) and it is make status bar as I want. Bu

相关标签:
13条回答
  • 2020-12-23 13:41
    fun showTransparentStatusbar() {
            activity!!.window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN)
        }
    
    
        fun removeStatusbarFlags() {
            activity!!.window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        }
    
    0 讨论(0)
  • 2020-12-23 13:42

    Try this and wont regret it

    window.decorView.systemUiVisibility = (
         View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
         View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    )
    
    0 讨论(0)
  • 2020-12-23 13:47

    Scroll down to check how the end result looks like

    First of all, define your styles.xml something like this-

    styles.xml

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    </style>
    

    DO NOT add the following line

    <item name="android:windowTranslucentStatus">true</item>

    Adding above line will NOT shift the layout up when the soft keyboard is shown on a Dialog with an EditText

    Then override this style in v21 and v23 styles like this-

    v21/styles.xml

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
         <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    </style>
    

    v23/styles.xml

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
         <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    </style>
    

    Activity code - Kotlin

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setFlags(
                LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                LayoutParams.FLAG_LAYOUT_NO_LIMITS
        )
        setContentView(R.layout.YOUR_LAYOUT_RESOURCE_ID)
        .
        .
    .
    }
    

    Activity code - Java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(
                LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                LayoutParams.FLAG_LAYOUT_NO_LIMITS
        )
        setContentView(R.layout.YOUR_LAYOUT_RESOURCE_ID)
        .
        .
        .
    }
    

    End result

    0 讨论(0)
  • 2020-12-23 13:48

    using mikepenz's comment

    what I exactly working code (converted to kotlin) below here.

    // at AppCompatActivity, min SDK is 16, I tested api 25
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    
        if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        }
        if (Build.VERSION.SDK_INT >= 19) {
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        }
        if (Build.VERSION.SDK_INT >= 21) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window.statusBarColor = Color.TRANSPARENT
        }
    
        setContentView(R.layout.activity_main)
    }
    
    0 讨论(0)
  • 2020-12-23 13:48

    You can use like this to hide status bar and navigation bar

    WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
        getWindow().setAttributes(attributes);
    

    and for showing the navigationBar again use this

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    
        }
    

    the color is grey for me, maybe you can force it to your primary color

    0 讨论(0)
  • 2020-12-23 13:49
    getWindow().setFlags(
        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
    )
    
    <style name="TranslucentStatusBar" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
    </style>
    
    0 讨论(0)
提交回复
热议问题