[英]How to remove top status bar black background
我想創建一個真正的全屏活動,但屏幕頂部總是有一個黑色狀態欄。 安卓 9.0。
我已經嘗試了幾乎所有我能在谷歌和現有應用程序中找到的類似工作。 清單、代碼、樣式、AS 示例全屏活動,都試過了。
樣式.xml:
<style name="AppThemeA" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
顯現:
<activity android:name=".ScreenActivity" android:theme="@style/AppThemeA" />
布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/rootLayout"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Kotlin(注釋行已嘗試但失敗):
class ScreenActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
//setTheme(R.style.AppThemeDetector)
super.onCreate(savedInstanceState)
//window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
//window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
//window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_FULLSCREEN
/*
window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_FULLSCREEN
)*/
setContentView(R.layout.activity_screen_detector)
...
}
}
[[[[ 解決方案 ]]]]
找到了造成這種情況的原因。 您應該在設置 -> 顯示中將應用程序設置為全屏應用程序。
https://www.gottabemobile.com/how-to-enable-full-screen-apps-on-galaxy-s10
所以固定。 感謝你的幫助。
我在通過凹口或切口區域顯示內容時遇到問題。 在文檔中找到了這個:
LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES - 內容以縱向和橫向模式呈現到剪切區域。
對我來說,關鍵是活動風格中的這一行:
// Important to draw through the cutouts
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
我想以沉浸式模式顯示圖像,當我單擊它時,系統 UI(狀態和導航欄)應該會顯示出來。
這是我的完整解決方案:
1- 在 Activity 中顯示/隱藏系統 UI 的方法
private fun hideSystemUI() {
sysUIHidden = true
window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
// Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // Hide nav bar
or View.SYSTEM_UI_FLAG_FULLSCREEN // Hide status bar
)
}
private fun showSystemUI() {
sysUIHidden = false
window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION // layout Behind nav bar
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // layout Behind status bar
)
}
2-確保在您的 xml 布局的根視圖中
android:fitsSystemWindows="false"
3- 全屏 Activity 的樣式將在狀態/導航欄出現時為它們提供半透明背景:
<style name="FullscreenTheme" parent="AppTheme">
<item name="android:actionBarStyle">@style/FullscreenActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">@null</item>
<item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
<item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
<item name="android:statusBarColor">#50000000</item>
<item name="android:navigationBarColor">#50000000</item>
// Important to draw behind cutouts
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
</style>
<style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
<item name="android:background">@color/sysTransparent</item>
</style>
在您的活動中添加此方法
public static void hideSystemUI() {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
在添加這個之前調用這個super.onCreate(savedInstanceState);
onCreate
hideSystemUI();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
如果您的設備有攝像頭缺口,則使用上述屬性。
除了您的代碼之外,您只需在活動中的 setContentView 之前添加此代碼
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
這是從Android Q開始實現邊到邊顯示的方法。代碼受此博客啟發。
首先,在您的styles.xml
,修改AppTheme
如下:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
...
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
假設,我們需要讓MainActivity
全屏,然后在MainActivity.kt
中:
window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_FULLSCREEN
)
此外,請查看此處的文檔以啟用全屏模式。
所以感謝這個視頻,我得到了解決方案。
首先應用程序主題應該擴展一個 NoActionBar 主題
<style name="Theme.YourApp" parent="Theme.MaterialComponents.DayNight.NoActionBar"></style>
之后在 Activity 的 onCreate 方法(在這種情況下使用 viewBinding)應該實現如下。
private lateinit var binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.attributes.layoutInDisplayCutoutMode = WindowManager
.LayoutParams
.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
}
WindowCompat.setDecorFitsSystemWindows(window, false)
binding = ActivityMainBinding.inflate(layoutInflater)
WindowInsetsControllerCompat(window, binding.root).let { controller ->
controller.hide(WindowInsetsCompat.Type.systemBars())
controller.systemBarsBehavior = WindowInsetsControllerCompat
.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
setContentView(binding.root)
}
這對我有用。 希望它也適合你。
首先,在您的 styles.xml 中,修改 AppTheme,如下所示:
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
並在您的清單文件中提及。
<activity
android:name=".activities.FullViewActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen"
/>
嘗試這個:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getResources().getColor(R.color.colorPrimary));
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
Window window = mContext.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
int statusBarHeight = (int) dpToPx(24);
View view = new View(mContext);
view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
view.getLayoutParams().height = statusBarHeight;
((ViewGroup) window.getDecorView()).addView(view);
view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
}
dpToPx
public float dpToPx(float dp) {
return (dp * Resources.getSystem().getDisplayMetrics().density);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.