2
我有兩個表:TableA和TableB。 TableA的有第一排,高度爲22和表B具有高度的第一排77.我想等同於表A的第一行至表B的第一行,爲此我使用下面的代碼:Android:通過ViewTreeObserver設置視圖高度
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]);
}
});
}
但我懷疑這是否正確?
因爲當我看着logcat時,它總是打印我的日誌,但如果我刪除textViewA.setHeight(heightB[0]);
它只打印一次日誌。
完美!你是天才先生Bartosz Lipinski。謝謝 )) – gigs