调用setContentView()后没有数据
目前我正在更新我的应用程序金钱就是时间,如果我'遇到了以下问题。我想支持横向和纵向,因此我创建了两种不同的布局。
我的第一个问题是,轮换后,应用程序重新启动,并且刷新了设置的数字。
在 stackoverflow 上找到这个问题后,这个问题很容易解决。
我已经针对该问题实现了以下解决方案:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setGui();
}
我已经使用以下代码对其进行了测试,其中轮子的布局根据布局进行调整。一切又都像它应该的那样工作。当我改变方向时,数字不会改变。
public void setGui() {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
initWheel(R.id.money1, 0, 9, "%1d", moneyWidthPortrait, textSizePortrait);
...
} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
initWheel(R.id.money1, 0, 9, "%1d", moneyWidthLandscape, textSizeLandscape);
...
}
}
但我也想更改背景和布局(填充和边距),因此我添加了以下两行:
public void setGui() {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(portrait);
...
} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(landscape);
...
}
}
但是在调用 setContentView 后,所有数据似乎再次丢失,并且在更改手机方向后轮子重置为零。
在方向和布局更改期间,最好避免轮子重置为“0”?
编辑:回答后编辑
我删除了:
android:configChanges="keyboardHidden|orientation"
并添加了:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("WheelMoney1", getWheelValue(R.id.money1));
...
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
getWheel(R.id.money1).setCurrentItem(savedInstanceState.getInt("WheelMoney1"), false);
...
}
并实现了 layout-land
和 layout-port
来处理两种不同的布局。
Currently i'm working on an update of my app money is time were I've ran into the following problem. I would like to support landscape and portrait orientation so I've created two different layouts.
My first problem was that after rotation the application restarted and the number set where flushed.
This was easy to fix after finding the this question here on stackoverflow.
I've implemented the following solution for that problem:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setGui();
}
I've tested it with the following code where the layout of the wheels are adjusted depending on the layout. Which again all works like it should. When i change the orientation, the numbers don't change.
public void setGui() {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
initWheel(R.id.money1, 0, 9, "%1d", moneyWidthPortrait, textSizePortrait);
...
} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
initWheel(R.id.money1, 0, 9, "%1d", moneyWidthLandscape, textSizeLandscape);
...
}
}
But I also want to change the background and the layout (padding and margins) so I've added the following two lines:
public void setGui() {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(portrait);
...
} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(landscape);
...
}
}
But after calling setContentView all the data seems to be lost again and the wheels are reset to zero after changing the phones orientation.
What would be the best to avoid that the wheels are reset to '0' during orientation and layout change?
EDIT: Edits after answer
I've removed:
android:configChanges="keyboardHidden|orientation"
and added:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("WheelMoney1", getWheelValue(R.id.money1));
...
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
getWheel(R.id.money1).setCurrentItem(savedInstanceState.getInt("WheelMoney1"), false);
...
}
And implemented layout-land
and layout-port
for handling the two different layouts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您需要实现 onSaveInstanceState< /a> 和 onRestoreInstanceState
It sounds like you need to implement onSaveInstanceState and onRestoreInstanceState
更改方向时,您不需要调用
setContentView
。如果您将布局以相同的名称保存在正确的布局目录下,Android 将为您执行此操作。例如:当您的 Activity 切换方向时,Android 将加载正确的布局。
You shouldn't need to call
setContentView
when changing orientations. Android will do this for you if you save your layout with the same name under the correct layout directory. For example:When your Activity switches orientation, Android will load the correct layout.