我在本教程中学习Android:
http://gamecodeschool.com/android-projects/但有一个问题。在某些游戏中,您需要在XML文件中设置全屏分辨率。我正在使用该教程,但是当我将其安装在设备上时,我的游戏崩溃了。它只是因为这个XML文件。我非常恼火,我尝试自己学习但仍然总是在这个全屏幕上出错。这是代码
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:0)
试试这个。
清单中的方式1 .set
<activity
...
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
java代码中的方式2 .set
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// add this before setContentView methood ,and after super
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT);
setContentView(R.layout.activity_main);
}
清单中的方式3 .set并在样式中设置
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="theme_fullScreen" parent="android:Theme.Black">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
</resources>
清单中的
<activity android:name=".LoginActivity"
android:theme="@style/theme_fullScreen"/>
注意强>
android:theme
。{/ li>的Activity
getWindow().setFlags(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT);
答案 1 :(得分:0)
public class ActivityName extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
AndroidManifest.xml
文件<activity android:name=".ActivityName"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>
<activity android:name=".ActivityName"
android:label="@string/app_name"
android:theme="@android:style/Theme.AppCompat.Light.NoActionBar"/>
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="theme_fullScreen" parent="android:Theme.Black">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
</resources>
答案 2 :(得分:0)
其他答案是正确的,但我提供了一个我正在使用的解决方案,甚至在通知或任何其他焦点更改时,它仍保持原样或恢复到原始状态。
将此内容放入您的活动中
声明此变量
private int currentApiVersion;
将其放入onCreate
currentApiVersion = Build.VERSION.SDK_INT;
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_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN;
if (currentApiVersion >= Build.VERSION_CODES.KITKAT) {
getWindow().getDecorView().setSystemUiVisibility(flags);
final View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
decorView.setSystemUiVisibility(flags);
}
}
});
}
以下方法onCreate
@SuppressLint("NewApi")
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (currentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus) {
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_IMMERSIVE_STICKY
|View.SYSTEM_UI_FLAG_FULLSCREEN);
}
}
这将为您提供最佳的全屏活动,就像游戏活动一样。