我需要使状态栏透明。我正在使用getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
实现我想要的状态栏效果。但它也会影响导航栏:导航栏也变成了透明的,getWindow().setNavigationBarColor(Color.BLACK)
无效。
有没有办法只使状态栏透明而不影响导航栏?
我需要使状态栏透明。我正在使用getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
实现我想要的状态栏效果。但它也会影响导航栏:导航栏也变成了透明的,getWindow().setNavigationBarColor(Color.BLACK)
无效。
有没有办法只使状态栏透明而不影响导航栏?
这对我起作用
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
)
样式.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>
状态栏会是透明或半透明的,导航栏则不会
希望这可以帮到你!
使用mikepenz的评论,我精确地编写了以下代码(已转换为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)
}
首先,将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)
.
.
.
}
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
fun showTransparentStatusbar() {
activity!!.window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
fun removeStatusbarFlags() {
activity!!.window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
试试这个,你不会后悔的。
window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
)
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);
}
对我来说,颜色是灰色的,也许你可以将它强制设置为你的主要颜色。
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
对我来说,上面的标志起作用了。导航按钮可见,状态栏和操作栏被隐藏。
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
无法工作。测试设备为Nexus 5。
<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"/>
对于所有对解决方案/黑客感兴趣的人,因为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()当然是您布局的根
结果为