布局高度不更新,直到另一个事件被触发 [英] Layout height doesn't update until another event is triggered

查看:128
本文介绍了布局高度不更新,直到另一个事件被触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设置为 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 :)

这篇关于布局高度不更新,直到另一个事件被触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆