我只能为状态栏设置FLAG_LAYOUT_NO_LIMITS吗?

时间:2015-10-26 13:43:05

标签: android android-5.0-lollipop android-statusbar

我需要制作透明状态栏。我正在使用getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS),它是我想要的状态栏。但它也会影响导航栏:它变得透明,getWindow().setNavigationBarColor(Color.BLACK)什么都不做。

是否有办法只制作透明状态栏而不是导航栏?

13 个答案:

答案 0 :(得分:18)

这对我有用

getWindow().setFlags(
    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
)

styles.xml

<style name="TranslucentStatusBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">true</item>
</style>

v21 \ styles.xml

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

状态栏将是透明或半透明的,导航栏将不会

希望这会有所帮助!

答案 1 :(得分:10)

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

答案 2 :(得分:7)

使用mikepenz's comment

我在这里完全正常工作的代码(转换为kotlin)。

// 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)
}

答案 3 :(得分:3)

你可以这样使用 隐藏状态栏和导航栏

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

再次使用此

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

    }

对我来说颜色是灰色的,也许你可以将它强制为原色

答案 4 :(得分:3)

fun showTransparentStatusbar() {
        activity!!.window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN)
    }


    fun removeStatusbarFlags() {
        activity!!.window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
    }

答案 5 :(得分:2)

  

向下滚动以查看最终结果的外观

首先,定义您的styles.xml像这样-

  

styles.xml

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

请勿添加以下行

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

在带有EditText的对话框中显示软键盘时,添加以上行不会增加布局。

然后在v21 v23样式中覆盖此样式-

  

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>

活动代码-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)
    .
    .
.
}

活动代码-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

答案 6 :(得分:2)

尝试一下,不会后悔

window.decorView.systemUiVisibility = (
     View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
     View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
)

答案 7 :(得分:1)

我找到了解决方案兄弟

<style name="transparent" parent="Theme.AppCompat.Light.NoActionBar">//AppCompat is the key; You can choose any other theme than the light-theme, but stick to AppCompat
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    //Other styling(optional)
</style>

然后将此透明主题应用于您的活动清单

 <activity
      ...
      android:theme="@style/transparent"/>

答案 8 :(得分:1)

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

只是标志以上为我工作。导航按钮可见,状态栏和操作栏被隐藏。

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

不工作。测试设备nexus 5.

答案 9 :(得分:1)

对于所有对解决方法/黑客感兴趣的人,因为与此问题相关的Android API简直太糟糕了。不要使用任何系统窗口标志,将负边距设置为布局的根,然后使用setStatusBarColor来使StatusBar透明。

statusBarHeight = getResources().getDimensionPixelSize(R.dimen.margin_24dp);
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId != 0) {
    statusBarHeight = getResources().getDimensionPixelSize(resourceId);
}

FrameLayout.LayoutParams rootParams = (FrameLayout.LayoutParams) binding.getRoot().getLayoutParams();
rootParams.topMargin = -statusBarHeight;
binding.getRoot().setLayoutParams(rootParams);

getWindow().setStatusBarColor(getResources().getColor(R.color.transparent));

其中binding.getRoot()当然是布局的根

结果是

enter image description here

答案 10 :(得分:0)

是的,您可以在Style.xml中使用此代码

<style name="transparent" parent="Theme.AppCompat.Light">//AppCompat is the key; You can choose any other theme than the light-theme, but stick to AppCompat

    <item name="colorPrimaryDark">#00000000</item>//THIS is the important part.
    //Other styling(optional)
</style>

然后将其应用于您的布局,只需在根布局(视图)中添加以下行:

android:theme="@style/transparent"

答案 11 :(得分:0)

D:\home\site\wwwroot

答案 12 :(得分:0)

这适用于Samsung S10 +(Pie),Oppo F7和Oppo F5S(Oreos):

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

按照原始发布者的要求,应用程序区域在顶部扩展,并覆盖状态栏(时钟所在的位置)。但是android导航(虚拟)按钮仍保留在屏幕底部,而应用程序则位于android按钮的顶部。