处理方向变化android
我有一个表,每行包含许多图像。每行的图像数量是根据图像宽度和屏幕宽度动态决定的。当我正常使用手机时,图像在手机屏幕上有间隔。当手机方向改变时,所有图像都会显示在同一行。我应该明确处理这种情况下的方向变化吗?
// 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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于该代码何时运行,但您通常不需要执行任何特殊操作。
当Android检测到方向变化时,系统将重新创建您的Activity,并且它将有机会根据新配置执行onCreate并重新布局并重新渲染所有内容。
使用 android
android:configChanges
应该小心使用,而不是您想到的第一个选项,因为在配置更改后系统会更好地为您选择合适的资源(并且您将无论如何,都必须处理其他形式的配置更改的代码路径)。看:
http://developer.android.com/guide/topics/resources/runtime -changes.html
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:configChanges="orientation|keyboardHidden"
放在 AndroidManifest.xml 文件中的
上,然后覆盖onConfigurationChanged 检测方向变化。
Yes. One way is to put
android:configChanges="orientation|keyboardHidden"
on your<activity>
in the AndroidManifest.xml file, and then overrideonConfigurationChanged
to detect the orientation change.如果您想保留一行中显示的图像数量,您可以使用GridView。通过这种方式,您可以控制每行的列数。
If you want to persist the number of images displayed in one row you can use the GridView. In this way you can control the number of columns per line.