设备更改方向android时更改用户界面(Change User interface when device change the orientation android)

编程入门 行业动态 更新时间:2024-10-22 04:46:21
设备更改方向android时更改用户界面(Change User interface when device change the orientation android)

我有一个屏幕,当设备是景观和肖像时,显示不同的用户界面。 它显示一个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.xml

In 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.

更多推荐

本文发布于:2023-08-07 01:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458836.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户界面   方向   设备   android   Change

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!