我有一个屏幕,当设备是景观和肖像时,显示不同的用户界面。 它显示一个ListView当设备是肖像时,它显示一个GridView当设备是lanscape时。 它看起来像youtube应用程序或谷歌玩商店的应用程序。
感谢您阅读和回答我的问题。
编辑:我想添加更多关于我的问题的细节:
我正在使用片段来管理这个视图。 我需要保存所有数据,并在旋转时保存在该视图中(避免重新加载大量数据)。 我试图在清单中添加android:configChanges,并在这个片段中使用onConfigurationChanged。 但这不是成功。我希望看到具体的例子或详细的解决方案。 但任何答案都是适当的。 谢谢
I have a screen, that show different user interface when device is lanscape and portrait. It shows a ListView when device is portrait, and it shows a GridView when device is lanscape. It looks like youtube app or google play store app.
Thanks for reading and answering my question.
EDIT: I want to add more details about my question:
I am using fragment to manage this view. I need save all data and in this view when it rotate (avoid reloading a lot of data). I tried to add android:configChanges on manifest and use onConfigurationChanged in this fragment. But it was not success.I hope see the particular example or a detail solution. But any answer is appropriated. Thanks
最满意答案
有两件事情来发展这种功能。
首先,你将不得不做出两种不同的布局:
res |- layout |- mylayout.xml |- layout-land.xml |- mylayout.xml在layout/mylayout.xml文件中,在你需要的地方包含一个ListView ,并在layout-land/mylayout.xml中包含你的GridView 。
当您调用setContentView时,Android将根据当前屏幕方向自动选择正确的布局。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mylayout); mListView = (ListView) findViewById(R.id.mylistview); mGridView = (GridView) findViewById(R.id.mygridview); // !!!!!!! // mListView will be null in landscape // mGridView will be null in portrait // !!!!!!! } setContentView(R.layout.mylayout);现在,在代码中,您需要检查if / else语句的方向。
要检测您处于哪个方位,请遵循以下状态:
在res/values/bool.xml创建一个文件
<resources> <bool name="isLandscape">false</bool> </resources>在res/values-land/bool.xml创建一个文件
<resources> <bool name="isLandscape">true</bool> </resources>现在,在您的活动中,您可以在每次需要时轻松测试,如果处于横向或纵向模式并应用相应的操作
boolean landscape = getResources().getBoolean(R.bool.isLandscape); if (landscape) { // do something with your GridView } else { // do something with your ListView }要回复您的编辑:
片段与此类活动非常相似。
如果要在方向更改期间保存数据,请遵循有关此主题的官方文档 ,尤其是在配置更改期间保留对象的部分。
正如文档中所解释的,您不应该在Manifest中使用android:configChanges :
处理配置更改可能会使其使用替代资源变得更加困难,因为系统不会自动为您应用它们。 当您必须避免由于配置更改而重新启动并且不推荐用于大多数应用程序时,应将此技术视为最后的手段。
There is two things to develop this functionnality.
First you will have to make two different layouts:
res |- layout |- mylayout.xml |- layout-land.xml |- mylayout.xmlIn the layout/mylayout.xml file, include a ListView where you need it and in layout-land/mylayout.xml, include your GridView.
Android will select automatically the correct layout, depending of the current screen orientation, when you call setContentView.
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mylayout); mListView = (ListView) findViewById(R.id.mylistview); mGridView = (GridView) findViewById(R.id.mygridview); // !!!!!!! // mListView will be null in landscape // mGridView will be null in portrait // !!!!!!! } setContentView(R.layout.mylayout);Now, in code you will need to check in which orientation you are to make if/else statements.
To detect in which orientation you are, follow the following states:
create a file in res/values/bool.xml
<resources> <bool name="isLandscape">false</bool> </resources>create a file in res/values-land/bool.xml
<resources> <bool name="isLandscape">true</bool> </resources>Now in your activity you can easily test, every time you need, if you are in landscape or portrait mode and apply the corresponding action
boolean landscape = getResources().getBoolean(R.bool.isLandscape); if (landscape) { // do something with your GridView } else { // do something with your ListView }To respond to your edit:
Fragments are very similar to Activities for this.
If you want to save data during orientation change, follow the official documentation on this topic, especially the part on Retaining an Object During a Configuration Change.
You should not use the android:configChangesin Manifest as explained in the documentation:
Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.
更多推荐
发布评论