Android 布局,横向和纵向上有不同的项目
我在 res/layout-land 和 res/layout 中有 main.xml。有一个由 12 个图标组成的网格,3 x 4(分别为宽度和高度)。如果用户旋转到横向,我想用 3 x 2 的 ViewPager 和 3 x 2 的两页显示它们。因此,在 res/layout 中有一个包含 12 个项目的网格,在 res/layout-land 中有一个 ViewPager,我需要为其提供一个 PagerAdapter。
我的问题是:假设我正在使用 configChanges="orientation",我该怎么做?或者更具体地说,例如,我在哪里为 ViewPager 提供其 PagerAdapter?
I have main.xml in res/layout-land and in res/layout. There's a grid of 12 icons, 3 by 4 (width, height, respectively). If the user rotates to landscape, I want to display them with a ViewPager of 3 by 2, and 3 by 2 in two pages. So in the res/layout there's a grid of 12 items and in the res/layout-land there's a ViewPager, to which I need to give a PagerAdapter.
My question is: under the assumption I'm working with configChanges="orientation", how do I do that? Or more specifically, Where for example do I give the ViewPager its PagerAdapter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建两个布局文件,并在 onCreate() 的开头(就在 super.onCreate(bundle) 行之后),在该行周围放置一个 if/else 语句:
看起来像这样:
从那时起,无论您的 java代码会有所不同,只需将其包装在相同的条件中即可。如果你的java代码非常依赖于纵向,那就有点啰嗦了,但我想不出更好的解决方案。
此外,根据您的应用程序及其功能,您可能需要在方向更改时重新创建活动。但同样,这可能没有必要。
我希望这有帮助
You could make two layout files and at the beginning of your onCreate() (just after super.onCreate(bundle) line), put an if/else statement around this line:
to look like this:
and from then on, wherever your java code would be different, just wrap it in the same condition. It's a little long-winded if your java code is very dependent on being in portrait, but I can't think of a better solution atm.
Also, depending on your application and it's functionality, you may need to recreate the activity on orientation change. But again, it may not be necessary.
I hope this helps