Android Orientation Change

2019-02-07 11:09发布

My tabbed app does not redisplay the view with an orientation change.

I added

android:configChanges="keyboardHidden|orientation"

to the main tab activity and to each activity in the manifest.

I added to each activity this method:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.active_alt);
    mColorLegendBtn = (Button) findViewById(R.id.colorbtn);
    mStatusView = (TextView) findViewById(R.id.celltitle1);
    TextView mStatusView1 = (TextView) findViewById(R.id.celltitle2);
    mStatusView1.setText(mStatusView1.getText()+"testcase1");
    mStatusView.setText(mStatusView.getText()+"testcase");
    initUI();
}

public void initUI() {
    l1 = (ListView) findViewById(R.id.ListView01);
    EfficientAdapter efficientAdapter = new EfficientAdapter(mContext);
    l1.setAdapter(null);
    l1.setAdapter(efficientAdapter);
}

On launch, the tabs, list, button and textview are displayed correctly. When I change the orientation in the emulator, only the tabs are displayed none of the other widgets, the screen is black.

What am I missing?

4条回答
男人必须洒脱
2楼-- · 2019-02-07 11:23

I had exactly this problem. After much trial and error, I eventually solved it by making a one-line change to the manifest.

The trick is to add

android:configChanges="orientation|keyboardHidden"

to your TabActivity's entry in the manifest. Leave all the child activities alone. Don't even bother implementing onConfigurationChanged(), not even in the TabActivity.

I don't know how or why this seems to work, but the effect is the layout is refreshed, and both the tabs and the child activity content are redrawn correctly in the new orientation.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-02-07 11:29

Could it just be that your layout doesn't work in the landscape/portrait mode? Try starting you app after rotating, check if that gives the same results. If so: fix your layout :D

查看更多
在下西门庆
4楼-- · 2019-02-07 11:30

With success I found that the best way to have screen changes with most control is to make your layout xml for landscape mode in a seperate xml like so:

res/layout-land/youractivity.xml

using /layout/ and /layout-land/ for your layouts as well as Graham Borland answer is golden.

  <activity android:name=".MainActivity"
                  android:label="@string/app_name"
                  android:screenOrientation="unspecified"
                  android:launchMode="standard"
                  android:configChanges="orientation|keyboardHidden"
                  >

the above snippet is what made mine work. :)

ohh I do believe the "unspecified" is what allows the system to do what it thinks is best...

Good luck!

查看更多
一纸荒年 Trace。
5楼-- · 2019-02-07 11:30

In Mono for Android with a target API greater then 13 I found that the line which would go inside the namespace but outside the Activity class:

[Activity (Label = "Viewer",  ConfigurationChanges = ConfigChanges.Orientation|ConfigChanges.ScreenSize)]

lead to the triggering of OnConfigurationChanged() even though changing the manifest had not.

查看更多
登录 后发表回答