[英]How to avoid changing of image and layout position when changing from portrait to landscape mode in android
I have problem while using lanscape mode in android.How to avoid changing of image position changing from portrait to landscape mode in android.Normally landscape4 and portrait mode is good.If we goes from one mode to another then position of images are changed. 我在android中使用lanscape模式时遇到问题。如何避免在android中将图像位置从纵向更改为横向模式。通常landscape4和纵向模式都很好。如果我们从一种模式转到另一种模式,则图像的位置会发生变化。 Please help me
请帮我
The activity in which you are showing image, register that Activity in Android Manifest file as: 您正在显示图像的活动,将Android Manifest文件中的Activity注册为:
<activity
android:name="com.android.YourActivity"
android:screenOrientation="portrait">
</activity>
In this way, it will force the activity to stay in Portrait mode only. 这样,它将强制活动仅保持纵向模式。
The orientation changes causes the running activity to restart.The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration. 方向更改会导致运行活动重新启动。重新启动行为旨在通过使用与新设备配置匹配的备用资源自动重新加载应用程序来帮助您的应用程序适应新配置。 Hence resource intensive.
因此资源密集。
Depending on how you want you app to behave and the device performance you can handle the orientation change(config changes as a whole) in following ways: 根据您希望应用程序的行为方式和设备性能,您可以通过以下方式处理方向更改(配置整体更改):
Fixed orientation: If you want your app to have a fixed orientation in landscape or portrait mode. 固定方向:如果您希望应用在横向或纵向模式下具有固定方向。 For this you need to set this attribute in the manifest file under the activity tag.
为此,您需要在活动标记下的清单文件中设置此属性。
android:screenOrientation="portrait" or android:screenOrientation="landscape"
Handling the Configuration Change Yourself: You can handle the orientation change yourself if your application doesn't need to update resources during a specific configuration change and you have a performance limitation that requires you to avoid the activity restart, then you can declare that your activity handles the configuration change itself, which prevents the system from restarting your activity. 处理配置更改:如果您的应用程序在特定配置更改期间不需要更新资源并且您有性能限制要求您避免重新启动活动,则可以自己处理方向更改,然后您可以声明您的活动处理配置更改本身,这会阻止系统重新启动您的活动。
For this you need to declare this in the manifest file: 为此,您需要在清单文件中声明:
<activity android:name=".MyActivity" android:configChanges="orientation|keyboardHidden" android:label="@string/app_name"/>
and you need to override the onConfigurationChanged() method of the activity. 并且您需要覆盖活动的onConfigurationChanged()方法。
** use this as the last resort as every thing needs to be handled by you. **将此作为最后的手段,因为您需要处理所有事情。
Default behavior: You can let android handle the config changes choosing the alternative resources from the resource folder. 默认行为:您可以让android处理配置更改,从资源文件夹中选择备用资源。 You just need to provide these alternate resources for landscape and portrait mode.
您只需要为横向和纵向模式提供这些备用资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.