我试图在设备旋转后找到内容视图的高度。下面的代码在启动时和轮换后打印ContentView.bottom。如果我以纵向模式启动设备,旋转到横向模式,然后旋转回纵向模式,它将打印:989,989,565。初始989是正确的,其他两个数字是错误的,它们是旋转前ContnetView的大小。
在打印ContentVew的大小之前,有没有办法检测到因旋转而导致的布局更改?
活动设置此标志:
android:configChanges="keyboardHidden|orientation"
public class TempProjActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
printContentBottom();
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
//this occurs when device is rotated or keyboard is exposed/hidden
super.onConfigurationChanged(newConfig);
printContentBottom();
}
private void printContentBottom()
{
getWindow().findViewById(Window.ID_ANDROID_CONTENT).post(new Runnable() {
@Override
public void run() {
Log.e("", getWindow().findViewById(Window.ID_ANDROID_CONTENT).getBottom()+"");
}
});
}
}