为什么不调用onConfigurationChanged?
我正在写一个代码,更改方向时更改TextView的文本我正在使用onConfigurationChanged()侦听器来更改文本
override fun onConfigurationChanged(newConfig: Configuration?) { super.onConfigurationChanged(newConfig) val orientation : Int = getResources().getConfiguration().orientation val oTag = "Orientation Change" if(orientation == (Configuration.ORIENTATION_LANDSCAPE)){ mytext?.setText("Changed to Landscape") Log.i(oTag, "Orientation Changed to Landscape") }else if (orientation == (Configuration.ORIENTATION_PORTRAIT)){ mytext?.setText("Changed to Portratit") Log.i(oTag, "Orientation Changed to Portratit") }else{ Log.i(oTag, "Nothing is coming!!") } }
但即使在Log中也没有任何反应
我也已经将这个属性添加到我的清单文件中的活动
android:configChanges="orientation"
我在这里错过了什么? 还有没有其他办法可以做到这一点?
使用此代码来获取配置文件
override fun onConfigurationChanged(newConfig: Configuration?) { super.onConfigurationChanged(newConfig) if (newConfig != null) { if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); } } }
几件事情要尝试:
android:configChanges="orientation|keyboardHidden|screenSize"
而不是android:configChanges="orientation"
确保你没有调用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
任何地方。 这将导致onConfigurationChange()不会触发。