简体   繁体   English

Android onResume布局问题

[英]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 看起来像这样: 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 因此,当我开始一个新游戏(新的Intent)并按返回按钮时,屏幕布局已损坏,看起来真的很糟糕: 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. 我试图删除所有按钮并在onResume中重新填充布局,但是它不起作用,我真的不知道为什么。 The System.out works properly, but the layout resets only when I switch between apps. System.out可以正常工作,但是只有当我在应用程序之间切换时,布局才会重置。

Does anyone know the problem? 有人知道这个问题吗?

Edit: The OnClickListener of the background buttons changed the background picture of the clicked button. 编辑:背景按钮的OnClickListener更改了单击按钮的背景图片。 This OnClickListener still works in the horrible broken layout. 该OnClickListener仍然可以在残破的布局中使用。

@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(). 您不需要删除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. 您可能还想在此处设置所有onClickListners。 But anything that you set here, you should delete in onPause(). 但是您在此处设置的任何内容都应该在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); LinearLayout buttonBackgroundLayout =(LinearLayout)findViewById(R.id.buttonHintergrundLayout);

Move this to onCreate and make and use only one instance of buttonBackgroundLayout(Declare it globbaly) 将其移动到onCreate上,仅制作和使用buttonBackgroundLayout的一个实例(声明为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!? 在Startup上看起来很糟糕,从startet游戏中恢复后看起来很不错,并且在不同的应用程序之间切换之后看起来很糟糕... Wtf !?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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