Android的屏幕方向变化 [英] Android screen orientation change
问题描述
我在做节目,当我preSS一个按钮,这个改变背景颜色,但我有一个问题,当我改变屏幕方向再次这一变化的颜色以predefined,我不知道如何解决这个问题......这是我的code。
I'm making a program that when I press a button this change the background color but I have a problem with, when I change the screen orientation this change again the color to the predefined and I dont know how to solve this problem... Here is my code.
public class MainActivity extends Activity implements OnClickListener {
LinearLayout myLayout;
LinearLayout myLayout2;
// Declare UI elements
private Button firstButton, secondButton, thirdButton, fourthButton, fifthButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // Our only layout for this app is main.xml
myLayout = (LinearLayout) findViewById(R.id.myLayout);
myLayout2 = (LinearLayout) findViewById(R.id.myLayout2);
// Initialize the UI components
firstButton = (Button) findViewById(R.id.button1);
// When we creating a button and if we expect that to use for event handling we have to set the listener
firstButton.setOnClickListener(this);
secondButton = (Button) findViewById(R.id.button2);
secondButton.setOnClickListener(this);
thirdButton = (Button) findViewById(R.id.button3);
thirdButton.setOnClickListener(this);
fourthButton = (Button) findViewById(R.id.button4);
fourthButton.setOnClickListener(this);
fifthButton = (Button) findViewById(R.id.button5);
fifthButton.setOnClickListener(this);
}
}
推荐答案
的问题是,当取向改变时,该活动被重新启动。有多种方法可以解决这个问题。
The problem is that when the orientation is changed, the activity is restarted. There are multiple ways to fix that.
的一种方法是让这个活动的不会的重新启动时的方向改变。以下是如何做到这一点:
One way is to make it so that the activity won't restart when the orientation is changed. Here's how to do that:
添加的android:configChanges =方向|屏幕尺寸
你的<活动
标签,这是你的的AndroidManifest.xml
,就像这样:
Add android:configChanges="orientation|screenSize"
to your <activity
tag, which is in your AndroidManifest.xml
, like so:
<activity
android:name="UserIdActivity"
android:label="@string/app_name"
android:configChanges="orientation|screenSize" />
<一个href=\"http://stackoverflow.com/questions/20306809/android-screen-orientation-change/20306937#20306937\">Rastikan's回答做到了略有不同,但他这样做的方式不会API 12后的API工作(<一个href=\"http://stackoverflow.com/questions/5620033/onconfigurationchanged-not-getting-called\">source和这也)。我上面的方法,你实际上并不需要调用 onConfiguationChanged
在你的活动类。
Rastikan's answer did it slightly differently, but his way of doing it won't work for any API after API 12 (source and this too). My way above, you don't actually need to call onConfiguationChanged
in your activity class.
这样做会使用的onSaveInstanceState(捆绑savedInstanceState)
如的另一种方法Rastikan 做到了。
Another way of doing it would to use onSaveInstanceState(Bundle savedInstanceState)
like Rastikan did.
做的是让活动重启本身,而是然后使用 onResume()
方法来改变背景颜色,如果有必要的另一种方式,当活动被重新启动。像这样的:
Another way of doing it would be to let the activity restart itself, but then using the onResume()
method to change the background colour, if necessary, when the activity is restarted. Like this:
public boolean changeColor = false;
// set changeColor to be true whenever you change the background colour
public void onResume()
{
if (changeColor) {
// change the background color
}
}
这篇关于Android的屏幕方向变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!