本文介绍了当设备改变方向的android更改用户界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个屏幕,显示该不同的用户界面时,设备lanscape和肖像。它显示了一个的ListView 当设备是纵向,它显示了一个的GridView 当设备lanscape。它看起来像YouTube应用程序或谷歌Play商店应用。

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.

编辑:我想补充一下我的问题的详细信息:

I want to add more details about my question:

  • 在我使用的片段来管理这一观点。
  • 在我需要保存所有数据,并在此视图时,它旋转(避免重装大量的数据)。
  • 我尝试添加机器人:configChanges的清单和使用onConfigurationChanged在这个片段。但事实并非如此成功。

我希望看到具体的例子或详细解决方案。但是,任何答案拨款。谢谢

I hope see the particular example or a detail solution.But any answer is appropriated.Thanks

推荐答案

有两件事情来开发,此功能。

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

布局/ mylayout.xml 文件,包括的ListView 在你需要它和布局陆/ mylayout.xml ,包括你的的GridView

In the layout/mylayout.xml file, include a ListView where you need it and in layout-land/mylayout.xml, include your GridView.

Android将自动选择正确的布局,这取决于当前的屏幕方向,当你调用的setContentView

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);

现在,在code,你将需要检查你是哪个方向,使if / else语句。

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:

  1. 创建一个文件 RES /价值/ bool.xml

<resources>
    <bool name="isLandscape">false</bool>
</resources>

  • 创建 RES /值陆/ 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
    }
    

  • 要响应您的编辑:

    片段非常相似的活动此

    如果你想保存过程中方向变化的数据,按照关于这个主题,尤其是上Retaining对象在更改配置

    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.

    您不应使用安卓-changes.html#HandlingTheChange>在文档:

    处理配置改变自己可以使它更  很难使用替代资源,因为该系统并不  自动应用它们。该技术,应考虑  当你必须避免重新启动由于配置不得已  改变,因此不建议对大多数应用程序。

    这篇关于当设备改变方向的android更改用户界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-23 00:31
    查看更多