Android:通过ViewTreeObserver设置视图高度 [英] Android: set view height via ViewTreeObserver

查看:124
本文介绍了Android:通过ViewTreeObserver设置视图高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表:TableA和TableB. TableA的第一行高度为22,TableB的第一行高度为77.我想将TableA的第一行等同于TableB的第一行,为此,我使用以下代码:

I have two tables: TableA and TableB. TableA has the first row with height of 22 and TableB has the first row with height of 77. I want to equate first row of TableA to first row of TableB, for that purpose I use code below:

void resizeHeaderHeight() {
    final int[] heightA = new int[1];
    final int[] heightB = new int[1];

    TableRow TableA_Row = (TableRow) this.tableA.getChildAt(0);
    TableRow TableB_Row = (TableRow) this.tableB.getChildAt(0);

    final TextView textViewA = (TextView) TableA_Row.getChildAt(0);
    final TextView textViewB = (TextView) TableB_Row.getChildAt(0);


    ViewTreeObserver vto = textViewB.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            textViewA.getViewTreeObserver();
            textViewB.getViewTreeObserver();
            heightA[0] = textViewA.getMeasuredHeight();
            heightB[0] = textViewB.getMeasuredHeight();
            Log.d(TAG, "TableA_Row height = " + heightA[0]);
            Log.d(TAG, "TableB_Row height = " + heightB[0]);

            textViewA.setHeight(heightB[0]);
        }
    });
}

但是我怀疑这是否是正确的方法?

But I doubt whether this is the right approach or not ?

因为当我查看logcat时,它始终会打印我的日志,但是如果我删除textViewA.setHeight(heightB[0]);,它只会打印一次日志.

Because when I look in logcat, it always prints me my Logs, but if I remove textViewA.setHeight(heightB[0]); it prints Logs just once.

推荐答案

假设您的布局设计正确,而设置textViewB高度的方法就是您真正想要的方法...

Assuming your layout is correctly designed and this way of setting height of your textViewB is the one you really wanna go with...

您应该在不再需要OnGlobalLayoutListener时将其删除.您没有这样做,并且ViewTree布局中的任何更改都会调用onGlobalLayout回调.所以回答您的问题:ViewTreeObserver的使用方式不是最好的...

You should remove OnGlobalLayoutListener as soon as it's not needed anymore. You're not doing that, and the onGlobalLayout callback is getting called on any change in the ViewTree layout. So answering your question: the way you're using ViewTreeObserver is not the best...

这样会更好:

void resizeHeaderHeight() {
    TableRow TableA_Row = (TableRow) this.tableA.getChildAt(0);
    TableRow TableB_Row = (TableRow) this.tableB.getChildAt(0);

    final TextView textViewA = (TextView) TableA_Row.getChildAt(0);
    final TextView textViewB = (TextView) TableB_Row.getChildAt(0);

    textViewB.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int heightB = textViewB.getHeight();
            if (heightB > 0) {
                // removing OnGlobalLayoutListener
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
                    textViewB.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    textViewB.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }

                // setting height
                textViewA.setHeight(heightB);
            }
        }
    });
}

这篇关于Android:通过ViewTreeObserver设置视图高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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