我提到了这个链接。如果用户点击 EditText(例如 To: ),那么键盘将被弹出,同时用户可以滚动查看所有剩余的视图(例如:撰写、主题、发送按钮)那个屏幕。同样,在我的应用程序中,我有一项活动,即我有一些小部件或视图。假设如果用户单击我的 Activity 中的 Edittext,那么键盘会弹出,我可以滚动查看剩余的视图。但是如果我android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
在清单中给出这个属性我无法滚动查看剩余的视图但是如果给出属性android:theme="@android:style/Theme.NoTitleBar"
像这样在清单中我可以滚动查看剩余视图但该屏幕中有状态栏,这里我想要全屏,即使弹出键盘我也可以滚动查看剩余视图..?我必须为此做出哪些改变..?
34 回答
写在你的活动中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
在此处查看文档:https ://developer.android.com/training/system-ui/status.html
你的应用程序将全屏显示。没有状态栏,没有标题栏。:)
使用主题"Theme.NoTitleBar.Fullscreen"
并尝试设置"android:windowSoftInputMode=adjustResize"
活动,您可以在此处AndroidManifest.xml.
找到详细信息。
使用此代码在您的应用中隐藏状态栏并易于使用
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
ActionBar actionBar = getActionBar();
actionBar.hide();
}
如果您在一项活动中需要此功能,则必须在 setContentView 之前放入 onCreate:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.your_screen);
将此添加到您的活动类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
// some your code
}
将此用于您的Activity
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
如果您要隐藏状态栏,请在 onCreate(for Activity) 和 onCreateView/onViewCreated(for Fragment) 中执行此操作
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
并且不要忘记在退出活动时清除标志,否则访问此活动后您的整个应用程序将全屏显示。要清除在 onDestroy(for Activity) 或 onDestroyView(for Fragment) 中执行此操作
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
更改manifest.xml
文件中应用程序的主题。
android:theme="@android:style/Theme.Translucent.NoTitleBar"
void hideStatusBar() {
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
}
您可以使用此方法隐藏状态栏。这对于隐藏操作栏也很重要。在这种情况下,如果您从支持库(如 Appcompat)扩展了活动,则可以getSupportActionBar().hide(),或者您可以在上述方法之后简单地调用getActionBar().hide() 。谢谢
在 AndroidManifest.xml -> 要使用的活动内,添加以下内容:
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
//this is for hiding action bar
并在 MainActivity.java -> onCreate() 方法内,添加以下内容:
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//this is for hiding status bar
使用此代码:
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.youractivityxmlname);
由于 FLAG_FULLSCREEN 已从 android R 中弃用。您可以使用以下代码隐藏状态栏。
@Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.hide(WindowInsets.Type.statusBars())
} else {
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
}
您可以使用 styles.xml 隐藏
<resources>
<!-- Base application theme. -->
<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>
</style>
<style name="HiddenTitleTheme" parent="AppTheme">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
像这样在你的清单中调用它android:theme="@style/HiddenTitleTheme"
此代码隐藏状态栏。
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
隐藏操作栏写下这一行: -
requestWindowFeature(Window.FEATURE_NO_TITLE);
这两行可以一起写来隐藏操作栏和状态栏。所有这些行都必须在setContentView
方法中的方法调用之前编写onCreate
。
这对我来说是最好的解决方案,只需在你的 theme.xml 中写下这一行
<style name="MyApp" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
...
</style>
您可以通过使用xml将其颜色设置为 transperant 来隐藏状态栏。将statusBarColor项目添加到您的活动主题中:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Theme_AppCompat_Light_NoActionBar);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity__splash_screen);
}
如果您正在使用更高的 API,那么您可能已经注意到上述答案中提到的标志,FLAG_FULLSCREEN
即已SYSTEM_UI_FLAG_FULLSCREEN
弃用。
要覆盖整个屏幕,您可以定义自定义主题:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="ActivityTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowFullscreen">true</item>
</style>
</resources>
在你的活动中添加主题,你就完成了。manifest
android:theme="@style/ActivityTheme"
注意:您也可以直接在清单中添加预定义的@androidandroid:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen"
主题: . 在这种情况下,请确保您的活动Activity()
不扩展AppCompatActivity()
。
包括android api 30,这对我有用
if (Build.VERSION.SDK_INT < 16) {
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
} else if (Build.VERSION.SDK_INT < 30) {
window.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
actionBar?.hide()
} else {
window.decorView.windowInsetsController?.hide(WindowInsets.Type.statusBars())
}
这是关于在 Android 4.0 及更低版本和 Android 4.1 及更高版本上隐藏状态栏的官方文档
请看一下:
https://developer.android.com/training/system-ui/status.html
这个解决方案对我有用:)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= 19) {
getWindow().setFlags(AccessibilityNodeInfoCompat.ACTION_NEXT_HTML_ELEMENT, AccessibilityNodeInfoCompat.ACTION_NEXT_HTML_ELEMENT);
getWindow().getDecorView().setSystemUiVisibility(3328);
}else{
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
DataBindingUtil.setContentView(this, R.layout.activity_hse_video_details);
我知道我回答得太晚了。为此,我在相关的 java 文件中使用以下代码
Objects.requireNonNull(getSupportActionBar()).hide();
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If the Android version is lower than Jellybean, use this call to hide
// the status bar.
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
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();
}
setContentView(R.layout.activity_main);
}
...
}
我们无法阻止状态在 (4.4+) kitkat 或更高版本的设备中以全屏模式出现,因此请尝试破解以阻止状态栏扩展。
解决方案非常大,所以这里是SO的链接:
隐藏状态栏
private void hideSystemBars() {
WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(getWindow().getDecorView());
if (windowInsetsController == null) {
return;
}
// Configure the behavior of the hidden system bars
windowInsetsController.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
// Hide both the status bar and the navigation bar
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
}
显示状态栏
private void showSystemBars() {
WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(getWindow().getDecorView());
if (windowInsetsController == null) {
return;
}
// Configure the behavior of the hidden system bars
windowInsetsController.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
// Hide both the status bar and the navigation bar
windowInsetsController.show(WindowInsetsCompat.Type.systemBars());
}
用于带屏幕的顶级相机切割
<item name="android:windowLayoutInDisplayCutoutMode" tools:ignore="NewApi">shortEdges</item>
在 Style.xml 文件中添加或替换
<item name="android:statusBarColor">@android:color/transparent</item>
我坚持使用干净且可扩展的方法来显示和隐藏系统 UI,适用于不同的 Android Api 级别:
object SystemBarsCompat {
private val api: Api =
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> Api31()
Build.VERSION.SDK_INT == Build.VERSION_CODES.R -> Api30()
else -> Api()
}
fun hideSystemBars(window: Window, view: View, isImmersiveStickyMode: Boolean = false) =
api.hideSystemBars(window, view, isImmersiveStickyMode)
fun showSystemBars(window: Window, view: View) = api.showSystemBars(window, view)
fun areSystemBarsHidden(view: View): Boolean = api.areSystemBarsHidden(view)
@Suppress("DEPRECATION")
private open class Api {
open fun hideSystemBars(window: Window, view: View, isImmersiveStickyMode: Boolean = false) {
val flags = View.SYSTEM_UI_FLAG_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
view.systemUiVisibility = if (isImmersiveStickyMode) {
flags or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
} else {
flags or
View.SYSTEM_UI_FLAG_IMMERSIVE or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}
}
open fun showSystemBars(window: Window, view: View) {
view.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
}
open fun areSystemBarsHidden(view: View) = view.systemUiVisibility and View.SYSTEM_UI_FLAG_HIDE_NAVIGATION != 0
}
@Suppress("DEPRECATION")
@RequiresApi(Build.VERSION_CODES.R)
private open class Api30 : Api() {
open val defaultSystemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_BARS_BY_SWIPE
override fun hideSystemBars(window: Window, view: View, isImmersiveStickyMode: Boolean) {
window.setDecorFitsSystemWindows(false)
view.windowInsetsController?.let {
it.systemBarsBehavior =
if (isImmersiveStickyMode) WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
else defaultSystemBarsBehavior
it.hide(WindowInsets.Type.systemBars())
}
}
override fun showSystemBars(window: Window, view: View) {
window.setDecorFitsSystemWindows(false)
view.windowInsetsController?.show(WindowInsets.Type.systemBars())
}
override fun areSystemBarsHidden(view: View) = !view.rootWindowInsets.isVisible(WindowInsets.Type.navigationBars())
}
@RequiresApi(Build.VERSION_CODES.S)
private class Api31 : Api30() {
override val defaultSystemBarsBehavior = WindowInsetsController.BEHAVIOR_DEFAULT
}
}
例如,要隐藏系统栏,可以从 a 调用它Fragment
:
SystemBarsCompat.hideSystemBars(requireActivity().window, view)
我们可以使用 setSystemUiVisibility() 在 Android 4.1(API 级别 16)及更高版本中隐藏状态栏
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
根据 google 文档,如果状态栏被隐藏,我们不应该显示操作栏,因此如有必要也将其隐藏。
actionBar?.hide()
如果您使用的是Compose,那么您可以导入
implementation "com.google.accompanist:accompanist-systemuicontroller:0.17.0"
然后在你的屏幕上写
val systemUiController = rememberSystemUiController()
systemUiController.isStatusBarVisible = false
如果您参考Google 文档,您可以在 android 4.1 及更高版本中使用此方法,请在 setContentView() 之前调用此方法
public void hideStatusBar() {
View view = getWindow().getDecorView();
int uiOption = View.SYSTEM_UI_FLAG_FULLSCREEN;
view.setSystemUiVisibility(uiOption);
ActionBar actionBar = getActionBar();
if (actionBar != null) {
actionBar.hide();
}
}
用于Manifest
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen"
在 res -> values ->styles.xml 下
里面的style body标签粘贴
<item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);