有没有办法运行此代码而不创建循环

时间:2019-01-25 13:41:37

标签: java android

我有4个按钮,它们的文本会动态变化,这可能导致Buttons的高度不同,我的代码试图避免这种情况,但还会创建一个循环

    ViewTreeObserver observer= Button0.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {

                    int HighestButton;
                    HighestButton = Button0.getHeight();
                    Log.d("HighestButton", HighestButton+ " 0");
                    if(Button1.getHeight() > HighestButton){
                        HighestButton = Button1.getHeight();
                        Log.d("HighestButton", HighestButton+ " 1");
                    }
                    if(Button2.getHeight() > HighestButton){
                        HighestButton = Button2.getHeight();
                        Log.d("HighestButton", HighestButton+ " 2");
                    }
                    if(Button3.getHeight() > HighestButton){
                        HighestButton = Button3.getHeight();
                        Log.d("HighestButton", HighestButton+ " 3");
                    }

                    Button0.setHeight(HighestButton);
                    Button1.setHeight(HighestButton);
                    Button2.setHeight(HighestButton);
                    Button3.setHeight(HighestButton);

                }
            });

3 个答案:

答案 0 :(得分:0)

ViewTreeObserver observer = Button0.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {

                    if(Button1.getHeight() > Button0.getHeight()){
                        Button0.setHeight(Button1.getHeight());
                    }

                    if(Button2.getHeight() > Button1.getHeight()){
                        Button0.setHeight(Button2.getHeight());
                        Button1.setHeight(Button2.getHeight());
                    }

                    if(Button3.getHeight() > Button2.getHeight()){
                        Button0.setHeight(Button3.getHeight());
                        Button1.setHeight(Button3.getHeight());
                        Button2.setHeight(Button3.getHeight());
                    }
                }
            });

答案 1 :(得分:0)

设置按钮的高度后,只需像

那样从观察者中删除侦听器
ViewTreeObserver observer= Button0.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {

                    int HighestButton;
                    HighestButton = Button0.getHeight();
                    Log.d("HighestButton", HighestButton+ " 0");
                    if(Button1.getHeight() > HighestButton){
                        HighestButton = Button1.getHeight();
                        Log.d("HighestButton", HighestButton+ " 1");
                    }
                    if(Button2.getHeight() > HighestButton){
                        HighestButton = Button2.getHeight();
                        Log.d("HighestButton", HighestButton+ " 2");
                    }
                    if(Button3.getHeight() > HighestButton){
                        HighestButton = Button3.getHeight();
                        Log.d("HighestButton", HighestButton+ " 3");
                    }

                    Button0.setHeight(HighestButton);
                    Button1.setHeight(HighestButton);
                    Button2.setHeight(HighestButton);
                    Button3.setHeight(HighestButton);

                    if (observer.isAlive()) {
                        observer.removeGlobalOnLayoutListener(this);
                    }

                }
            });

答案 2 :(得分:0)

动态更改文本是什么意思?您可以在按钮中调用设置文本,

所以我认为您可以在设置没有ViewTreeObserver的文本后立即执行