处理方向变化android

发布于 2024-12-17 18:57:49 字数 573 浏览 2 评论 0原文

我有一个表,每行包含许多图像。每行的图像数量是根据图像宽度和屏幕宽度动态决定的。当我正常使用手机时,图像在手机屏幕上有间隔。当手机方向改变时,所有图像都会显示在同一行。我应该明确处理这种情况下的方向变化吗?

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黯淡〆 2024-12-24 18:57:49

这取决于该代码何时运行,但您通常不需要执行任何特殊操作。

当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

薯片软お妹 2024-12-24 18:57:49

是的。一种方法是将 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 override onConfigurationChanged to detect the orientation change.

薄荷港 2024-12-24 18:57:49

如果您想保留一行中显示的图像数量,您可以使用GridView。通过这种方式,您可以控制每行的列数。

<GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:isScrollContainer="true" 
        android:numColumns="3"
        android:verticalSpacing="10dp" 
        android:horizontalSpacing="10dp"
        />

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.

<GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:isScrollContainer="true" 
        android:numColumns="3"
        android:verticalSpacing="10dp" 
        android:horizontalSpacing="10dp"
        />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文