【问题标题】:"values-w320dp-land" folder doesn't apply on screen landscape rotation“values-w320dp-land”文件夹不适用于屏幕横向旋转
【发布时间】:2016-02-03 21:53:47
【问题描述】:

我的项目资源下有以下 2 个值文件夹:

  1. 这是我的dimens.xml 用于portrait 320dp 宽度屏幕的显示:

  1. 这是我的dimens.xml 用于landscape 320dp 宽度屏幕的显示:

我得到的是应用程序启动时的正确显示(横向显示和纵向显示):

  • 应用程序以纵向模式正确启动:

  • 应用程序以横向模式正确启动:

现在我的问题是当我在应用程序运行时旋转屏幕时(假设应用程序首先以纵向模式启动),应用程序似乎没有从应该是“values-w320dp-land”的正确值文件夹中检索值" :

反之亦然,如果我在 ladscape 模式下启动应用程序后旋转屏幕,应用程序似乎不会从应该是“values-w320dp-port”的正确值文件夹中检索值,我得到这个:

更新 1

这是AndroidManifest.xml中的活动声明:

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name=".utils.json.VolleyApplication" >

        <activity
            android:name="com.test.momo.CategoriesActivity"
            android:label="@string/app_name"
            android:configChanges="orientation|screenSize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <!--Monitor for Changes in Connectivity-->
                <!--<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>-->
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

</application>

这是我在活动布局中的 GridView 声明:

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/gridview_categories"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:columnWidth="@dimen/gridview_row_item_size"
            android:numColumns="@integer/num_columns"
            android:verticalSpacing="@dimen/gridview_row_item_spacing"
            android:horizontalSpacing="@dimen/gridview_row_item_spacing"
            android:stretchMode="none"
            android:layout_marginLeft="@dimen/gridview_row_item_spacing"
            android:layout_marginRight="@dimen/gridview_row_item_spacing"
            android:scrollbars="none"/>

更新 2

所以我终于非常接近解决方案了。现在,当我在运行时旋转屏幕时,在我对此处的活动代码进行了一些更改后,我的 GridView 会正确地调整为纵向或横向模式:

@Override
public void onResume() {
    super.onResume();

    gridView.setNumColumns(getResources().getInteger(R.integer.num_columns));
    gridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.gridview_row_item_size));
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    gridView.setNumColumns(getResources().getInteger(R.integer.num_columns));
    gridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.gridview_row_item_size));
}

除此之外,这是我在运行时(且仅当)我将屏幕从纵向旋转到横向时得到的结果:

如您所见,我的 GridView 的第一个元素似乎保留了纵向模式的高度和宽度值。而其余的 GridView 元素显示正确。

我在这里错过了什么?

【问题讨论】:

  • 您的活动在AndroidManifest.xml 中有android:configChanges="orientation"stackoverflow.com/questions/7818717/… 怎么解决?删除该行并开始正确处理您的异步作业。祝你好运!
  • @EugenPechanec :感谢您的回答,但从清单中删除 android:configChanges="orientation" 会导致活动在每次屏幕旋转时重新创建,即使在每次屏幕旋转后显示正确的显示,我也需要阻止这种情况.如何让我的应用程序在运行时在屏幕旋转时正确显示,同时防止在每次屏幕旋转事件时重新创建活动?
  • 请查看我帖子中的 UPDATE 2。

标签: android android-resources screen-orientation screen-resolution landscape-portrait


【解决方案1】:

当您在清单中指定android:configChanges 时,活动将收到onConfigurationChanged 回调。一个实现可能如下所示:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // Absolutely detach any adapters from lists.
    // RecyclerView adapters have caches that need to be cleared.
    // We can reuse adapter but not widgets.
    mList.setAdapter(null);

    // Inflate new view hierarchy.
    setContentView(R.layout.the_same_layout_as_in_on_create);

    // Update variables so they point to newly inflated widgets
    ButterKnife.bind(this); // update variables to point 

    // Reattach adapters
    mList.setAdapter(mAdapter);

    // Here follows everything else that touches widgets in onCreate...
}

请注意,如果我更改手机上的语言,该活动仍会完全重启,因为它不在 android:configChanges 中。

另请注意,使用此方法时,活动不遵循生命周期 onPause-onStop-onDestroy-onCreate-onStart-onResume。

我的建议是学习如何正确保存状态和片段,然后重新创建整个活动。最昂贵的操作是膨胀布局,无论如何您都必须这样做。

【讨论】:

  • 请查看我帖子中的 UPDATE 2。
  • @MouradJEMAÏL 重新更新 2:您是否还注意到,当您以横向启动活动并旋转到纵向时,工具栏仍然只有 48dp 高度和小字体?反之亦然。停止试图发明一些宏大的计划,如何以边缘或根本没有速度提升的名义来破坏框架。只需重新创建活动。请问是什么阻止您重新创建活动?这可能是一个很好的 SO 问题。
  • 为什么还要在 onResume 中设置列​​号?那是 onCreate 材料(你设置布局的地方)。
  • 重新创建活动意味着重新加载 GridView 并重复所有过程以从 Internet 检索所有数据。所以这就是为什么它不适合我。请在单击加号按钮时查看 Google Play 商店应用程序,您会得到一个不错的 GridView,即使您旋转屏幕,它也会调整到屏幕方向而不会再次刷新。这正是我的目的。
  • 那么请阅读我的回答。你必须重新创建布局,没有办法。但是您不必重新加载数据。适配器保持不变,仍然加载数据,或者您的异步任务仍在运行并且可以访问非空适配器实例。
猜你喜欢
  • 1970-01-01
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-24
  • 1970-01-01
  • 2011-11-15
相关资源
最近更新 更多