在 android kitkat 中使用新的 IMMERSIVE 模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21135445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using new IMMERSIVE mode in android kitkat
提问by Saeid Yazdani
I want to make an activity to go into IMMERSIVE mode and hide top and buttom system bars as soon as it starts.
我想让一个活动进入 IMMERSIVE 模式并在它启动后立即隐藏顶部和底部系统栏。
In developers site of android they say I should use setSystemUiVisibility()
and provide SYSTEM_UI_FLAG_IMMERSIVE
and SYSTEM_UI_FLAG_HIDE_NAVIGATION
.
在Android开发者网站,他们说我应该使用setSystemUiVisibility()
,并提供SYSTEM_UI_FLAG_IMMERSIVE
和SYSTEM_UI_FLAG_HIDE_NAVIGATION
。
How can I do this in the OnCreate()
method of the activity? I think the setSystemUiVisibility
is not provided in the Activity class and it should happen in a view. Is there a workaround?
我怎样才能OnCreate()
在活动的方法中做到这一点?我认为setSystemUiVisibility
Activity 类中没有提供它,它应该在视图中发生。有解决方法吗?
UPDATE
更新
ok According to doorstuck I added the following lines but I dont see any changes, navigation bar and buttom buttons are still visible :
好的,根据doorstuck,我添加了以下几行,但没有看到任何更改,导航栏和按钮仍然可见:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
}
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE);
}
//Rest of activity code
回答by doorstuck
Get the decor view:
获取装饰视图:
getWindow().getDecorView().setSystemUiVisibility(...)
Remember that the arguments are bit flags. Only call the method above once:
请记住,参数是位标志。只调用一次上面的方法:
getWindow().getDecorView().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);
回答by swisscoder
Chris Banes gist shows a nice Helper Class we can use to set the immersive mode for all Versions from HoneyComb to Lollipop https://gist.github.com/chrisbanes/73de18faffca571f7292.
Chris Banes gist 展示了一个不错的 Helper Class,我们可以使用它为从 HoneyComb 到 Lollipop https://gist.github.com/chrisbanes/73de18faffca571f7292 的所有版本设置沉浸式模式。
Update: I tried get it from his github repo to include in my project, but i had to clone the gist files into my project and adjsut the packagename. If someone knows how to include it properly as a dependency, u r welcome to help me.
更新:我尝试从他的 github 存储库中获取它以包含在我的项目中,但我不得不将 gist 文件克隆到我的项目中并调整包名。如果有人知道如何正确地将其作为依赖项包含在内,欢迎帮助我。
I added it in my FullScreenActivity i want to use the ImmersiveStickyMode like this:
我将它添加到我的 FullScreenActivity 中,我想像这样使用 ImmersiveStickyMode:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
final int flags = 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;
SystemUiHelper uiHelper = new SystemUiHelper(this, SystemUiHelper.LEVEL_IMMERSIVE ,flags);
uiHelper.hide();
}
回答by George Papatheodorou
Much nicer and credit to William J. Francis:
对威廉·J·弗朗西斯 (William J. Francis) 的评价要好得多:
public class GameActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
/* my code you dont need this
assets=getAssets();
sGame= new GameView(this, GAME_WIDTH, GAME_HEIGHT);
setContentView(sGame);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
*/
getWindow().getDecorView().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 // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
@Override
protected void onDoSomethingOtherImportantThing(){
...
}
}
回答by Rahul Parihar
You can create global function to go into immersive mode like:
您可以创建全局函数以进入沉浸式模式,例如:
public static void enableImmersiveMode(final View decorView) {
decorView.setSystemUiVisibility(setSystemUiVisibility());
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
decorView.setSystemUiVisibility(setSystemUiVisibility());
}
}
});
}
public static int setSystemUiVisibility() {
return 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;
}
Above code will also control system UI visibility change. Hope this will help you.
上面的代码还将控制系统 UI 可见性的变化。希望这会帮助你。
回答by anand krish
android:immersive="true"will hide the Bottom system bars
android:immersive="true"将隐藏底部系统栏
<application>
<activity
android:name=".CarrierActivity"
android:label="@string/app_name"
android:excludeFromRecents="true"
android:immersive="true"
android:configChanges="orientation|keyboardHidden|screenSize">
<intent-filter>
<action android:name="com.example.SetupWiz.SUW_CARRIER"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
回答by sud007
Answer has already been given but here is How to utilize Immersive mode.
已经给出了答案,但这里是如何利用沉浸式模式。
In your activity:
在您的活动中:
just before setContentview().......
就在 setContentview() 之前......
call the method: toggleHideyBar();
调用方法:toggleHideyBar();
Follow documentation documentation from Developer android and copy this method in your Activity.
遵循Developer android 的文档文档并在您的 Activity 中复制此方法。