横向尺寸被忽略

时间:2018-01-19 14:41:59

标签: android

我有一个应用程序,我在Manifest上强制执行肖像:

<activity
            android:name=".MyActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="portrait"
            android:label="@string/launcher_name"
            android:launchMode="singleTop"
            android:windowSoftInputMode="adjustPan">

</activity>

我确实在某些条件下使用以下方式手动上架:

 private fun triggerPortrait() {
        this@DashboardListActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    }

    private fun triggerLandscape() {
        this@DashboardListActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
    }

现在我希望某些元素在方向为横向时改变它们的大小,所以我有这两个dimens.xml

梦诗-land.xml

<resources>
    <dimen name="scoreboard_height">24dp</dimen>
</resources>

梦诗-port.xml

<resources>

    <dimen name="scoreboard_height">16dp</dimen>
</resources>

然而,在一个和另一个之间的切换根本不起作用,只有正在使用的景观。我想这与我强制执行肖像有关,但是有办法解决这个问题吗?

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以从android:configChanges="orientation"的清单条目中删除MyActivity。但由于这是不可取的,并且声明具有此属性的配置将阻止活动重新启动,而是活动保持运行并调用其onConfigurationChanged()方法,并且您应该注意配置更改并应用逻辑您想要覆盖onConfigurationChanged()

所以这是如何手动完成的,

将这些维度值复制到一个全局可用的值/ dimens.xml文件

值/ dimens.xml

<resources>
    <dimen name="scoreboard_height_land">24dp</dimen>
    <dimen name="scoreboard_height_port">16dp</dimen>
</resources>

更新方向尺寸以通过其键指向这些值(以防您重新使用它们,否则在您的应用程序中没有所有这些逻辑且这些区域不会中断)

值-脊/ dimens.xml

<resources>
    <dimen name="scoreboard_height">@dimen/scoreboard_height_land</dimen>
</resources>

值端口/ dimens.xml

<resources>
    <dimen name="scoreboard_height">@dimen/scoreboard_height_port</dimen>
</resources>

覆盖onConfigurationChanged以设置记分板的新高度

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

public void setHeightOfScoreboardViewBasedOnOrientation(int orientation) {

    // change the id to match what you have in your xml (since i dont know it at the time of writing this)
    View myScoreboardView = findViewById(R.id.myScoreboardView);

    // get the layout params and set a new height, then set it back n the view
    ViewGroup.LayoutParams myScoreboardViewLayoutParams = myScoreboardView.getLayoutParams();
    if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
        myScoreboardViewLayoutParams.height = getResources().getDimensionPixelSize(R.dimen.scoreboard_height_land);
    } else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
        myScoreboardViewLayoutParams.height = getResources().getDimensionPixelSize(R.dimen.scoreboard_height_port);
    }
    myScoreboardView.setLayoutParams(myScoreboardViewLayoutParams);

}

注意: 您可以使用setHeightOfScoreboardViewBasedOnOrientationtriggerPortraittriggerLandscapeConfiguration.ORIENTATION_PORTRAIT方法调用Configuration.ORIENTATION_LANDSCAPE方法。但是处理configChanges会更有趣,特别是当你的活动在后台并且方向发生变化时!