简体   繁体   中英

Android onResume Layout Problems

I have a problem with my menu.

I have a background layout filled with buttons, vertical and horizontal. Looks like this: http://s7.directupload.net/file/d/3081/sewg79tr_png.htm

So when I start a new game (new Intent) and press the back button, the screen layout is broken and looks really terrible: http://s14.directupload.net/file/d/3081/ewfeidya_png.htm

When I switch to another app and switch back to the menu, the layout looks like it looks on startup. Everything is fine.

I am trying to remove all buttons and refill the layout in onResume, but it does not work and I really don't know why. The System.out works properly, but the layout resets only when I switch between apps.

Does anyone know the problem?

Edit: The OnClickListener of the background buttons changed the background picture of the clicked button. This OnClickListener still works in the horrible broken layout.

@Override
public void onResume() {
    super.onResume();
    LinearLayout buttonBackgroundLayout = (LinearLayout) findViewById(R.id.buttonHintergrundLayout);
    //int count = buttonBackgroundLayout.getChildCount();
    //System.out.println(count);
    //for (int i = 0; i < count; i++) {
    //    View child = buttonBackgroundLayout.getChildAt(i);
    //    if (child instanceof View) ((ViewGroup) child).removeAllViews();
    //}
    buttonBackgroundLayout.removeAllViewsInLayout();
    buttonBackgroundLayout.invalidate();

    createButtonImages(breite);
    createBackgroundButtons(breite);
    System.out.println("WOOOHOOO");
}

Edit:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    buttonBackgroundLayout = (LinearLayout) findViewById(R.id.buttonHintergrundLayout);

    createButtons();        

    DisplayMetrics display = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(display);
    breite = display.widthPixels;

    createButtonImages(breite);
    createBackgroundButtons(breite);
}

You shouldn't need to remove all of the views in onResume(). This should be all you need to do, so far as the layout is concerned.

@Override
public void onResume() {
    super.onResume();
    setContentView(R.id.buttonHintergrundLayout);

}

You also probably want to set all onClickListners here. But anything that you set here, you should delete in onPause(). However, setting the view is essentially free, especially if you are just using a view that is in place already.

LinearLayout buttonBackgroundLayout = (LinearLayout) findViewById(R.id.buttonHintergrundLayout);

Move this to onCreate and make and use only one instance of buttonBackgroundLayout(Declare it globbaly)

I guess the problem is of multiple instances of the view getting created

Hmm on another phone (Samsung) it's exactly the opposite. On Startup it looks bad, after resuming from a startet game it looks good and after switching between different apps it looks bad... Wtf!?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM