gpt4 book ai didi

android - 在 Activity 中永久隐藏导航栏

转载 作者:IT老高 更新时间:2023-10-28 21:44:41 25 4
gpt4 key购买 nike

是否可以永久删除 Activity 上的导航栏?我想删除显示在平板电脑屏幕按钮上的按钮栏,而不是操作栏。 Here .

我知道不建议这样做,我没有做出这个决定,但我需要这样做。在我的布局上,还有另一个按钮可以离开 Activity 。我的应用程序的其余部分可以具有并且确实具有导航栏。

我找到了这段代码并对其进行了一些调整。问题是即使我隐藏了导航栏,也会留下一个黑色空间。我猜系统在计算屏幕尺寸时会考虑导航栏?

public static class Content extends ImageView implements View.OnSystemUiVisibilityChangeListener, View.OnClickListener, ActionBar.OnMenuVisibilityListener {
Activity mActivity;
TextView mTitleView;
Button mPlayButton;
SeekBar mSeekView;
boolean mAddedMenuListener;
boolean mMenusOpen;
boolean mPaused;
boolean mNavVisible;
int mLastSystemUiVis;

Runnable mNavHider = new Runnable() {
@Override public void run() {
setNavVisibility(false);
}
};

public Content(Context context, AttributeSet attrs) {
super(context, attrs);
setOnSystemUiVisibilityChangeListener(this);
setOnClickListener(this);
}

public void init(Activity activity, TextView title, Button playButton,
SeekBar seek) {
// This called by the containing activity to supply the surrounding
// state of the video player that it will interact with.
mActivity = activity;
mTitleView = title;
mPlayButton = playButton;
mSeekView = seek;
mPlayButton.setOnClickListener(this);
setPlayPaused(true);
}

@Override protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (mActivity != null) {
mAddedMenuListener = true;
mActivity.getActionBar().addOnMenuVisibilityListener(this);
}
}

@Override protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mAddedMenuListener) {
mActivity.getActionBar().removeOnMenuVisibilityListener(this);
}
}

@Override public void onSystemUiVisibilityChange(int visibility) {
// Detect when we go out of nav-hidden mode, to clear our state
// back to having the full UI chrome up. Only do this when
// the state is changing and nav is no longer hidden.
int diff = mLastSystemUiVis ^ visibility;
mLastSystemUiVis = visibility;
if ((diff&SYSTEM_UI_FLAG_HIDE_NAVIGATION) != 0
&& (visibility&SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
setNavVisibility(true);
}
}

@Override protected void onWindowVisibilityChanged(int visibility) {
super.onWindowVisibilityChanged(visibility);

// When we become visible or invisible, play is paused.
setPlayPaused(true);
}

@Override public void onClick(View v) {
if (v == mPlayButton) {
// Clicking on the play/pause button toggles its state.
setPlayPaused(!mPaused);
} else {
// Clicking elsewhere makes the navigation visible.
setNavVisibility(true);
}
}

@Override public void onMenuVisibilityChanged(boolean isVisible) {
mMenusOpen = isVisible;
setNavVisibility(true);
}

void setPlayPaused(boolean paused) {
mPaused = paused;
mPlayButton.setText(paused ? R.string.play : R.string.pause);
setKeepScreenOn(!paused);
setNavVisibility(true);
}

void setNavVisibility(boolean visible) {
int newVis = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| SYSTEM_UI_FLAG_LAYOUT_STABLE;
if (!visible) {
newVis |= SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_FULLSCREEN
| SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}

// If we are now visible, schedule a timer for us to go invisible.
if (visible) {
Handler h = getHandler();
if (h != null) {
h.removeCallbacks(mNavHider);
if (!mMenusOpen && !mPaused) {
// If the menus are open or play is paused, we will not auto-hide.
h.postDelayed(mNavHider, 1500);
}
}
}

// Set the new desired visibility.
setSystemUiVisibility(newVis);
mTitleView.setVisibility(visible ? VISIBLE : INVISIBLE);
mPlayButton.setVisibility(visible ? VISIBLE : INVISIBLE);
mSeekView.setVisibility(visible ? VISIBLE : INVISIBLE);
}

}

最佳答案

有一个从 KitKat (4.4.2) 开始的解决方案,称为沉浸式模式:https://developer.android.com/training/system-ui/immersive.html

基本上,您应该将此代码添加到您的 onResume() 方法中:

View decorView = getWindow().getDecorView();
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);

关于android - 在 Activity 中永久隐藏导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16713845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com