模拟器不使用横向变体
Emulator does not use the landscape variant
所以我有一个非常简单的项目,只有几张图片。我创建了一个风景变化,并改变了图像的位置。
问题是,当 运行在模拟器或设备上运行项目时,使用默认横向布局而不是变体。
如果它 运行 当模拟器处于横向位置时,它确实使用了变体,但弄乱了纵向。
我唯一改变的是 styles.xml 具有:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
作为应用主题。
我是 Android Dev 的新手。所以它可能相当简单。
非常感谢任何帮助。
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.test">
<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:configChanges="orientation|screenSize|keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
它应该是什么样子:
https://i.stack.imgur.com/UXQCd.png
不应该的样子:
https://i.stack.imgur.com/dS54W.png
我找到了解决办法。
这涉及保持行:
android:configChanges="orientation|screenSize|keyboardHidden" >
在 MainActivity.java 中只需添加一个 onConfigurationChanged 函数,如下所示:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//retrieving resources for the variations
setContentView(R.layout.activity_main);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
//retrieving resources for the variations
setContentView(R.layout.activity_main);
}
}
这是我能想到的最佳解决方案。可能有更好的方法,因为这似乎有点多余。
所以我有一个非常简单的项目,只有几张图片。我创建了一个风景变化,并改变了图像的位置。
问题是,当 运行在模拟器或设备上运行项目时,使用默认横向布局而不是变体。
如果它 运行 当模拟器处于横向位置时,它确实使用了变体,但弄乱了纵向。
我唯一改变的是 styles.xml 具有:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
作为应用主题。
我是 Android Dev 的新手。所以它可能相当简单。
非常感谢任何帮助。
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.test">
<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:configChanges="orientation|screenSize|keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
它应该是什么样子:
https://i.stack.imgur.com/UXQCd.png
不应该的样子:
https://i.stack.imgur.com/dS54W.png
我找到了解决办法。
这涉及保持行:
android:configChanges="orientation|screenSize|keyboardHidden" >
在 MainActivity.java 中只需添加一个 onConfigurationChanged 函数,如下所示:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//retrieving resources for the variations
setContentView(R.layout.activity_main);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
//retrieving resources for the variations
setContentView(R.layout.activity_main);
}
}
这是我能想到的最佳解决方案。可能有更好的方法,因为这似乎有点多余。