问题
Based on the accepted answer here, I added this code to an Activity as what I hoped to be the first step in changing the assigned Layout for an Activity:
@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();
setContentView(R.layout.activity_settings_landscape);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_settings);
}
}
...and modified the Activity's element in AndroidManifest.xml. e.g., changing it from this:
<activity
android:name="hhs.app.SettingsActivity"
android:label="@string/title_activity_settings" >
</activity>
...becomes this:
<activity
android:name="hhs.app.SettingsActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/title_activity_settings" >
</activity>
(I added the "configChanges" jazz).
But, although mashing keypad.9 does indeed flip the orientation in the emulator, no toast is being raised (the breakpoint on the "if" line of onConfigurationChanged() is not being hit). What am I doing wrong or omitting?
Note: It made no difference when I removed "|keyboardHidden" from the "configChanges" line in AndroidManifest (it was just a hunch - I wondered if it meant "pay no attention to an orientation change made via the keyboard").
UPDATE
I tried araut's suggestion, and also the one from Jared Burrows here, but neither one works for me.
Cold this be simply an emulator problem? I have no device to test it with yet, so I can't empirically answer that question myself.
UPDATE 2
Based on what I read here, I thought maybe adding some OrientationEventListener jazz would solve this dilemma, but no!
This is the code I added (shown in context; what's new is the OrientationEventListener stuff):
public class SettingsActivity extends ActionBarActivity {
private OrientationEventListener mOrientationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
. . .
mOrientationListener = new OrientationEventListener(getApplicationContext()) {
@Override
public void onOrientationChanged(int orientation) {
Toast tostadaOrient = Toast.makeText(SettingsActivity.this,
orientation, Toast.LENGTH_SHORT);
tostadaOrient.setGravity(Gravity.CENTER, 0, 0);
tostadaOrient.show();
}
};
mOrientationListener.enable();
}
...but I still get no reaction (no toasts are raised from here or the onConfigurationChanged() event).
来源:https://stackoverflow.com/questions/23789605/why-is-my-orientation-change-going-unnoticed