我希望有人能帮助我。
我正在用Java开发一个Android游戏,我需要隐藏主屏幕的导航栏,但我找不到任何解决方案。我查了很多资料,甚至检查了这里提出的解决方案,但一无所获。
我已经在布局(juego.xml)中构建了我的地图,然后我在从视图(VistaJuego)扩展的java类中使用了它。另外,在这个类中,我定义了另一个类,名为 ThreadJuego。此类的目标是无限期地运行更新游戏物理的方法。
最后我有另一个名为 Juego 的类,它扩展自 AppCompactActivity。最后一个类加载 Vistajuego 的实例。
在下面找到类和布局的定义:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.richard_tfg.tankgame.VistaJuego
android:id="@+id/VistaJuego"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:background="@drawable/mapa_batalla2">
</com.richard_tfg.tankgame.VistaJuego>
这是布局的代码。下面我贴出一个VistaJuego的例子:
public class VistaJuego extends View {
private ThreadJuego thread = new ThreadJuego();
/* Do more things */
public VistaJuego(Context context, AttributeSet attrs){
/*Definitions of graphics*/
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
/* Definitions*/
...
...
...
thread.start();
}
protected synchronized void onDraw(Canvas canvas) {
/*Definitions*/
}
private void setFeatures(Grafico grafico, int angulo, int rotacion){
/*Definitions*/
}
protected synchronized void actualizaFisica(){
/*Update physics*/
}
class ThreadJuego extends Thread{
@Override
public void run() {
while(true){
actualizaFisica();
}
}
}
}
最后是我的Juego类的代码:
public class Juego extends AppCompatActivity {
VistaJuego vistaJuego;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.juego);
vistaJuego=findViewById(R.id.VistaJuego);
}
}
我试图用以下行隐藏导航栏:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); but it still keeps on the screen.
另外我尝试过使用下一个方法:
private void hideStatusAndNavBar() {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
);
}
并在 Juego 类的 onCreate 方法上调用它,但如果我这样做,我的设备上会出现两种不同的行为:
Honor 9X:我可以看到导航栏被隐藏,但所有图形都混合在一个角落,并且应用程序的行为很奇怪。
Samsung S10:应用程序总是失败,我在 Android Studio 的日志中看到的问题与定义 thread.start(); 有关。我不知道为什么问题是线程......
我搜索了很多信息但没有任何效果,所以如果有人可以帮助我,我将永远感激不已。
在 onCreate 中调用此方法
private void hideSystemBars() {
WindowInsetsControllerCompat controller = new WindowInsetsControllerCompat(
getWindow(), getWindow().getDecorView()
);
controller.hide(WindowInsetsCompat.Type.systemBars());
controller.setSystemBarsBehavior(
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
);
}
进口
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;
此方法将隐藏导航栏,并在用户滑动导航栏区域时显示它。
我不是专业/经验丰富的 Android 开发人员,但这样的东西可能对你有用:
// removal of status and navigation bar
if (Build.VERSION.SDK_INT >= 30) {
fullscreenContent.windowInsetsController?.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
} else {
// Note that some of these constants are new as of API 16 (Jelly Bean)
// and API 19 (KitKat). It is safe to use them, as they are inlined
// at compile-time and do nothing on earlier devices.
fullscreenContent.systemUiVisibility =
View.SYSTEM_UI_FLAG_LOW_PROFILE or
View.SYSTEM_UI_FLAG_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
您可以在“OnCreate”方法中添加此代码,其中“fullscreenContent”是保存所有屏幕数据的布局,在您的情况下应该是包含“VistaJuego”的布局,您可能需要将其添加到xml文件中,因为我不'不建议您使用根框架布局来实现此目的。
编辑:
在某些情况下,您可能需要在活动的
OnCreate
方法中使用此方法:
window?.apply {
statusBarColor = Color.TRANSPARENT
}
WindowCompat.setDecorFitsSystemWindows(window, false)