目前我有一个framelayout
,其背景颜色由它所拥有的textview
中的行数决定。如果行数超过2,那么我想要有不同的背景颜色。对于屏幕方向更改,我想重新计算onConfigurationChanged
方法中的背景颜色。当我拨打getLineCount()
时,我仍然可以获得方向更改之前的行数。换句话说,lineCount将从方向更改之前返回lineCount。
一个方向的改变总是会落后。这个问题有方法解决吗?
答案 0 :(得分:0)
使用
int lineCnt = textView.getLineCount();
handling orientation change 基本上,有两种方法可以处理它。 一个需要重新启动您的活动,另一个不需要。我已阅读建议重启活动的评论。
如果你
您可能会遇到这样一种情况,即重新启动应用程序并恢复大量数据可能成本高昂且用户体验不佳
您可以考虑通过执行以下操作来重新启动您的活动:
在xml中:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
活动
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
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();
}
}