Android: set view height via ViewTreeObserver

被刻印的时光 ゝ 提交于 2019-12-13 14:24:18

问题


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 ?

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.


回答1:


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

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...

This way would be better:

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);
            }
        }
    });
}


来源:https://stackoverflow.com/questions/34818093/android-set-view-height-via-viewtreeobserver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!