布局高度不更新,直到另一个事件被触发

问题描述:

我有一个设置为 A的LinearLayout(A)GONE 布局XML。我想实现的是点击一个按钮(B)时,在的LinearLayout(A)将被设置为可见键,然后我得到它的高度,更新的高度其父布局。我设法设置的LinearLayout(A)可见,也得到其高度,但是我不能直接更新其父布局的高度。从我目前的code的问题在于,另一UI事件触发后父布局将只更新本身(如微调下拉菜单打开后,软键盘显示等)。我真正想要的是父布局按钮(B)后直接更新自己被点击。

I have a LinearLayout(A) which is set to GONE in layout xml. What I want to achieve is that when a button(B) is clicked, the LinearLayout(A) will be set to VISIBLE and then I get its height to update the height of its parent layout. I managed to set the LinearLayout(A) to visible and also get its height, however I fail to update the height of its parent layout directly. The problem from my current code is that, the parent layout will only update itself after another UI Event is triggered (such as spinner drop down menu is opened, soft keyboard shown, etc.). What I really want is that the parent layout update itself directly after button(B) is clicked.

下面是我的code。

@Override
public void onClick(View v) {
    if(v==buttonB) {
        ShowLinearLayoutA();
    }
}

private ViewTreeObserver vto;

private void ShowLinearLayoutA() {
    LinearLayoutA.setVisibility(View.VISIBLE);
    if (vto == null) {
        vto = LinearLayoutA.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ParentLayout.getLayoutParams().height=LinearLayoutA.getHeight();
                ParentLayout.invalidate();
            }
        });
    }
}

我做了什么错了?

What did i do wrong ?

请原谅我的英语,并非常感谢你在前进。

Excuse my English and Thank you very much in advance.

编辑1:
我试着用 runOnUiThread ,但它不能正常工作

EDIT 1 : I tried using runOnUiThread but it doesn't work

我的编辑code

private void ShowLinearLayoutA() {
    LinearLayoutA.setVisibility(View.VISIBLE);
    if (vto == null) {
        vto = LinearLayoutA.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ParentLayout.getLayoutParams().height=LinearLayoutA.getHeight();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ParentLayout.invalidate();
                    }
                });
            }
        });
    }
}

编辑2:

我放弃了这个方法。相反,我使用的动画,并得到它的工作。
感谢:)

I gave up trying this method. Instead, I used animation and got it working. Thanks :)

我放弃了这个方法。相反,我使用的动画,并得到它的工作。感谢:)

I gave up trying this method. Instead, I used animation and got it working. Thanks :)