我如何使一个活动全屏?没有通知栏。
当前回答
对于那些使用AppCompact的人… style.xml
<style name="Xlogo" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
那就把名字写在你的载货单上…
其他回答
要使您的活动全屏执行以下操作:
// add following lines before setContentView
// to hide toolbar
if(getSupportActionBar()!=null)
getSupportActionBar().hide();
//to hide status bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
这将隐藏工具栏和状态栏。
但在某些情况下,你可能想用透明的背景显示状态栏,在这种情况下,这样做:
// add following lines before setContentView
// to hide toolbar
if(getSupportActionBar()!=null)
getSupportActionBar().hide();
// to make status bar transparent
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
一些其他的替代隐藏工具栏代替 getSupportActionBar () hide ():
通过改变应用主题的父元素来移除工具栏:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
如果你想从一个活动中删除工具栏,然后去manifest,在活动标签下添加这个:android:theme="@style/ theme . appcompat.light . noactionbar "
对于kotlin爱好者,为什么不使用扩展函数呢:
对于第一种情况:
fun AppCompatActivity.makeItFullScreenStatusBarVisible(){
supportActionBar?.hide()
window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
}
在setContentView之前调用这个:
makeItFullScreenStatusBarVisible()
第二题:
fun AppCompatActivity.makeItFullScreenStatusBarHidden(){
supportActionBar?.hide()
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
在setContentView之前调用它:
makeItFullScreenStatusBarHidden()
我想使用我自己的主题,而不是使用@android:style/ theme . notitlebar . fullscreen。但它并没有像这里的一些帖子所提到的那样工作,所以我做了一些调整来解决它。
在AndroidManifest.xml:
<activity
android:name=".ui.activity.MyActivity"
android:theme="@style/MyTheme">
</activity>
在styles.xml:
<style name="MyTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
</style>
注意:在我的情况下,我必须使用name="windowActionBar"而不是name="android:windowActionBar"在它正常工作之前。所以我只是使用了这两种方法,以确保我以后需要移植到新的Android版本。
如果你不想使用主题@android:style/ theme . notitlebar。因为你已经在使用你自己的主题,你可以使用android:windowFullscreen。
在AndroidManifest.xml:
<activity
android:name=".ui.activity.MyActivity"
android:theme="@style/MyTheme">
</activity>
在styles.xml:
<style name="MyTheme" parent="your parent theme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
提示:使用getWindow().setLayout()会搞砸你的全屏显示!注意,这个方法的文档说:
设置窗口的宽度和高度布局参数… 你可以把它们改成……用于制作非全屏窗口的绝对值。
http://developer.android.com/reference/android/view/Window.html#setLayout%28int,%20int%29
For my purposes, I found that I had to use setLayout with absolute parameters to resize my full screen window correctly. Most of the time, this worked fine. It was called by an onConfigurationChanged() event. There was a hiccup, however. If the user exited the app, changed the orientation, and reentered, it would lead to firing off my code which included setLayout(). During this re-entry time window, my status bar (which was hidden by the manifest) would be made to re-appear, but at any other time setLayout() would not cause this! The solution was to add an additional setLayout() call after the one with the hard values like so:
public static void setSize( final int width, final int height ){
//DO SOME OTHER STUFF...
instance_.getWindow().setLayout( width, height );
// Prevent status bar re-appearance
Handler delay = new Handler();
delay.postDelayed( new Runnable(){ public void run() {
instance_.getWindow().setLayout(
WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.FILL_PARENT );
}}, FILL_PARENT_ON_RESIZE_DELAY_MILLIS );
}
然后,窗口正确地调整大小,状态栏不会重新出现,不管触发这一事件。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
adjustFullScreen(newConfig);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
adjustFullScreen(getResources().getConfiguration());
}
}
private void adjustFullScreen(Configuration config) {
final View decorView = getWindow().getDecorView();
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
} else {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
}
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。