操作方向转变Android [英] Handling orientation changes android
问题描述
我具有包含数每行的图像的表。每行图像的数量决定基于所述图像的宽度和屏幕宽度的飞行。当我用手机正常,图像间隔在手机屏幕上。当手机取向改变,所有的图像显示在同一行上。我应该明确地处理方向改变这种情况?
//获取每列图像为基础的屏幕上的数字
// 解析度
显示显示= getWindowManager()getDefaultDisplay()。
INT屏幕宽度= display.getWidth();资源解析度= getResources();
可绘制可绘制= res.getDrawable(R.drawable.emo_im_happy);numberOfImagesPerRow =屏幕宽度/(drawable.getIntrinsicWidth()+ 10);INT numberOfRows =(int)的Math.ceil(numberOfEmotions
/ numberOfImagesPerRow);
在运行code时,这取决于,但通常不需要做什么特别的。
当Android的检测改变方向时,系统会重新创建活动,它会得到一个机会去通过的onCreate和重新布局,并根据新的配置重新渲染一切。
采用Android 的android:configChanges
应小心使用,而不是被你想到的第一个选项,因为系统将在为你选择合适的资源做得更好配置更改后(并且你必须处理其他形式的配置更改反正那code路径)。
请参阅:
http://developer.android.com/guide/topics/resources/runtime -changes.html
I have a Table containing a number of images per row. The number of images per row is decided on the fly based on the image width and screen width. When I use the phone normally, images are spaced on the phone screen. When the phone orientation changes, all the images appear on the same row. Should I explicitly handle the orientation changes for this case?
// get the number of images per column based on the screen
// resolution
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.emo_im_happy);
numberOfImagesPerRow = screenWidth / (drawable.getIntrinsicWidth() + 10);
int numberOfRows = (int) Math.ceil(numberOfEmotions
/ numberOfImagesPerRow);
It depends when that code is run, but you generally don't need to do anything special.
When Android detects an orientation change, the system will re-create your Activity and it will get a chance to go through onCreate and re-layout and re-render everything according to the new configuration.
Using android android:configChanges
should be used with care and not be the first option you think of, since the system will do a better job at selecting appropriate resources for you after a configuration change (and you'll have to handle that code path for other forms of configuration change anyways).
See: http://developer.android.com/guide/topics/resources/runtime-changes.html
这篇关于操作方向转变Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!