我想让我的活动全屏显示,顶部有状态栏,如下图所示:
我在
manifest
标签内的activity
中使用了此代码:
'android:theme="@style/Theme.AppCompat.Light.NoActionBar"'
但是我的视图不是从状态栏开始的,它看起来像这样:
我怎样才能让我的
activity
看起来像第一个?
我知道提出问题的人可能已经找到了自己的解决方案,但对于仍在寻找解决方案的人来说,这是一个非常简单的解决方案,但有一件事它有一个限制,直到
Kitkat
所以添加了一个条件
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
将这些添加到 styles.xml
中的基本应用程序主题
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
并将以下属性添加到您的父布局中:
android:fitsSystemWindows="false"
如果您不希望“半透明导航”透明,请使用以下代码使状态栏透明
在你的主题中:
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">true</item>
在您的创建活动中:
Window window = getWindow();
WindowManager.LayoutParams winParams = window.getAttributes();
winParams.flags &= ~WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
window.setAttributes(winParams);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
window?.decorView?.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
window.statusBarColor = Color.TRANSPARENT
window.setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.hide(WindowInsets.Type.statusBars())
}
else {
@Suppress("DEPRECATION")
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
}
val actionBar: ActionBar? = supportActionBar
if (actionBar != null) actionBar.hide()
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LOW_PROFILE
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
这个代码放在哪里?
override fun onCreate(savedInstanceState: Bundle?) {
/* Put above code here ..... */
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_slider)
}
注意
<style name="Theme.FullScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar">
1。您的活动主题
<style name="FullScreenTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowActivityTransitions">true</item>
</style>
2。无需将
android:fitsSystemWindows
属性添加到根布局 XML 文件中。
3.在您的活动中
将填充顶部添加到工具栏。我正在使用自定义工具栏,因此我向工具栏添加了状态栏高度的填充。
toolbar.setPadding(0, getStatusBarHeight(), 0, 0)
并且功能
getStatusBarHeight()
已添加到我的Utils class
中
fun Activity.getStatusBarHeight(): Int {
val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
return if (resourceId > 0) resources.getDimensionPixelSize(resourceId)
else Rect().apply { window.decorView.getWindowVisibleDisplayFrame(this) }.top
}
将下面的代码放入 onCreate() 中
private void setStatusBarTransparent() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
View decorView = window.getDecorView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
window.setStatusBarColor(Color.TRANSPARENT);
}
}
尝试这样
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
希望这会有所帮助
在 Kotlin 中,只需在您的 Activity 中使用以下代码,并在 Activity.xml 布局文件的父视图中添加 android:fitsSystemWindows="true" 。 如果您在 style.xml 中声明了全屏样式,这也将在导航栏上显示内容
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
window.setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
)
}
您可以在onCreate之后使用此代码
setTheme(android.R.style.Theme_Black_NoTitleBar_Fullscreen);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
您的活动将全屏显示并带有状态栏:)
@tahsinRupam 答案是正确的,并在 Kitkat 后版本上进行了测试。
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
如果您只想将其用于特定活动,请创建一个样式,并将其附加到您的清单样式上,如下所示。
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:theme="@style/Activity.Fullscreen.Theme" />
<style name="Activity.Fullscreen.Theme" parent="MyMaterialTheme.Base">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
对原创@tahsinRupam
竖起大拇指快乐编码干杯!
将此代码放入您的 Activity onCreate 中,这将隐藏状态栏
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();