开发者

View layout doesn't refresh on orientation change in android

I'd like my VIEW layout to be adjusted on orientation change.

My Manifest is set up with: android:configChanges="keyboardHidden|orientation" on the activity.

I have res/layout-land, res/layout-port.

I have onConfigurationChanged in my Activity (it get's called on rotation).

In my Activity I have a ViewFlipper and inside it I have LinearLayouts.

When I START the Activity in port or land mode, the View's layout is correct (according to my res/layout-???? files).

But when I'm already in the Activity and I rotate the phone, then my View layout is not changed. The screen is rotated, and the onCreate of the activity is not called, as it's supposed to be, the onConfigurationChanged is called in the Activity, but the screen is always redrawn with the LinearLayout that was loaded when the Activity started.

I have a feeling that I miss out something in the onConfigurationChanged, but don't know what. I guess I need to call some function on the ViewFlipper or on the LinearLayout that's inside. What functions should I call?

ViewFlipper flipper;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    flipper = new ViewFlipper(getApplicationContext());
    setContentView(flipper);
    initLayout();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(flipper);
}

void initLayout() {
    MyView myview = createMyView(0);
    flipper.addView(myview, 0);
    myview = createMyView(1);
    flipper.addV开发者_Python百科iew(myview, 1);
    myview = createMyView(1);
    flipper.addView(myview, 1);
}

I tried to add flipper = new ViewFlipper(getApplicationContext()); to onConfigurationChanged() but that made everything black after rotation. I guess I'll have to call initLayout, but that would recalculate everything (and that's exactly what I don't want to do, and that's why I'm trying to do it with onConfigurationChanged, as opposed to the android default "dropMyActivity & createANewActivity"

SOLUTION: Previously I thought I can trigger the layout to be refreshed on rotation, but it seems I have to re-create the layout with the current context (after the orientation change)

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.category_flipper);
    ViewFlipper oldFlipper = viewFlipper;
    viewFlipper = (ViewFlipper)findViewById(R.id.categoryFlipper);
    for (int i = 0; i < 3; i++) {
        final CardLayout cl = new CardLayout(this, cardData[i]);
        viewFlipper.addView(cl);
    }
    cardData[viewIndex].restorePlayer();
    viewFlipper.setDisplayedChild(viewIndex);
    oldFlipper.removeAllViews();
}


If flipper is a reference to the view that was initially installed, that's what's causing the problem. You need to pass the resource ID of the layout you want, not the old view from before the phone was rotated. The content view will then be bound to the new resource. Maybe something like this (modulo my guess at your resource identifiers):

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.flipper);
    flipper = (ViewFlipper) findViewById(R.id.flipper);
}


put that in your android manifest. android:configChanges="orientation"

in that case just try this and set your layout...

    if (Intraday.this.getResources (). getConfiguration (). orientation == Configuration.ORIENTATION_PORTRAIT){ 


    }
    else if (Intraday.this.getResources (). getConfiguration (). orientation == Configuration.ORIENTATION_LANDSCAPE) {      


    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜