1

我有一个图像查看器应用程序,可以处理具有大量缓存的非常大的图像。当用户旋转设备时,如果应用程序完全重新启动,则会导致打嗝。

如果我禁用方向更改,则设备旋转是无缝的,但我有一个侧边栏,横向和纵向方向不同。禁用方向更改后,侧边栏将保持之前的状态,占用大量空间。

有没有一种方法可以让布局方面在不完全重新启动活动的情况下更新旋转?


编辑(更多信息)


我正在添加更多信息以使我的原始问题更清楚,以便我回答。布局是这样的:

 ___ _
|   | |
| V |S|  Landscape
|___|_|

 ___
|   |
| V |
|___|   Portrait
|_S_|

V = 查看器 S = 侧边栏

侧边栏是同一个片段,但是它根据方向使用不同的布局。我最初的尝试是复杂的多布局设置。当我允许活动在方向更改时重新启动时,这工作正常。但是,由于我不想重新发布所有繁重的工作(或添加实例节省开销),我需要找到一种方法来调整方向更改的布局,而无需完全重新启动。

4

2 回答 2

1

将此行放入清单的活动标签中

android:configChanges="orientation"
于 2013-09-21T18:36:56.113 回答
1

我最终做的是调整布局以处理带有空容器的横向和纵向,这些容器稍后将填充适当的片段:

<RelativeLayout>
   ...
   <FrameLayout
       android:id="@+id/xmpRightContainer"
       android:layout_width="wrap_content"
       android:layout_height="match_parent"
       android:layout_alignParentRight="true"  >
   </FrameLayout>

   <FrameLayout
       android:id="@+id/xmpBottomContainer"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"  >
   </FrameLayout>
</RelativeLayout>

在 onConfigurationChanged 中:

    if (xmpFrag != null)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.remove(getSupportFragmentManager().findFragmentByTag(XmpFragment.FRAGMENT_TAG));
        ft.commit();
        fm.executePendingTransactions();
    }

    xmpFrag = new XmpFragment();
    int container;
    boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    if (isPortrait)
    {
        container = R.id.xmpBottomContainer;
    }
    else
    {
        container = R.id.xmpRightContainer;
    }

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(container, xmpFrag, XmpFragment.FRAGMENT_TAG);      
    ft.commit();
    fm.executePendingTransactions();

这完美地工作并且现在旋转设备对用户来说是无缝的。

于 2013-10-01T09:44:09.283 回答