gpt4 book ai didi

android - 景观维度被忽略

转载 作者:行者123 更新时间:2023-11-30 00:09:45 25 4
gpt4 key购买 nike

我有一个应用程序,我在 list 上强制使用肖像:

<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

dimens-land.xml

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

尺寸端口.xml

<resources>

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

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

enter image description here

enter image description here

enter image description here

最佳答案

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

所以这是手动完成的方法,

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

值/维度.xml

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

更新方向维度以通过其键指向这些值(以防万一您在应用中的其他地方重新使用它们而没有所有这些逻辑并且这些区域不会中断)

values-land/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);

}

注意:您可以使用 Configuration.ORIENTATION_PORTRAITConfiguration 从 triggerPortraittriggerLandscape 方法调用 setHeightOfScoreboardViewBasedOnOrientation 方法。 ORIENTATION_LANDSCAPE。但是处理 configChanges 学习起来更有趣,尤其是当你的 Activity 在后台堆栈中并且方向发生变化时!!!

关于android - 景观维度被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48343547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com