Fix GridLayout rounding issue

Fix TextView text centering
This commit is contained in:
hshristov
2015-08-10 14:32:24 +03:00
parent c0d47c8c3f
commit 467353d3d5
2 changed files with 28 additions and 22 deletions

View File

@@ -126,14 +126,18 @@ public class CommonLayoutParams extends FrameLayout.LayoutParams {
// Re-measure TextView because it is not centered if layout width is larger than measure width.
if (child instanceof android.widget.TextView) {
boolean canChangeWidth = lp.width < 0;
boolean canChangeHeight = lp.height < 0;
int measuredWidth = child.getMeasuredWidth();
int measuredHeight = child.getMeasuredHeight();
int width = right - left;
int height = bottom - top;
if (Math.abs(measuredWidth - width) > 1 || Math.abs(measuredHeight - height) > 1) {
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
int width = childRight - childLeft;
int height = childBottom - childTop;
if ((Math.abs(measuredWidth - width) > 1 && canChangeWidth) || (Math.abs(measuredHeight - height) > 1 && canChangeHeight)) {
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(canChangeWidth ? width : lp.width, MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(canChangeHeight ? height : lp.height, MeasureSpec.EXACTLY);
if (debuggable > 0) {
sb.setLength(0);
sb.append("remeasure ");

View File

@@ -305,20 +305,21 @@ public class GridLayout extends LayoutBase {
this.columnOffsets.add(paddingLeft);
this.rowOffsets.add(paddingTop);
int offset = this.columnOffsets.get(0);
int roundedOffset = this.getPaddingLeft();
float offset = this.columnOffsets.get(0);
int roundedOffset = paddingLeft;
int roundedLength = 0;
int actualLength = 0;
float actualLength = 0;
int size = this.helper.columns.size();
for (int i = 0; i < size; i++) {
ItemGroup columnGroup = this.helper.columns.get(i);
offset += columnGroup.length;
this.columnOffsets.add(offset);
actualLength = offset - roundedOffset;
roundedLength = Math.round(actualLength);
columnGroup.rowOrColumn._actualLength = roundedLength;
roundedOffset += roundedLength;
this.columnOffsets.add(roundedOffset);
}
offset = this.rowOffsets.get(0);
@@ -329,12 +330,13 @@ public class GridLayout extends LayoutBase {
for (int i = 0; i < size; i++) {
ItemGroup rowGroup = this.helper.rows.get(i);
offset += rowGroup.length;
this.rowOffsets.add(offset);
actualLength = offset - roundedOffset;
roundedLength = Math.round(actualLength);
rowGroup.rowOrColumn._actualLength = roundedLength;
roundedOffset += roundedLength;
this.rowOffsets.add(roundedOffset);
}
int columns = this.helper.columns.size();
@@ -442,7 +444,7 @@ class MeasureSpecs {
}
class ItemGroup {
int length = 0;
float length = 0;
int measuredCount = 0;
ItemSpec rowOrColumn;
ArrayList<MeasureSpecs> children = new ArrayList<MeasureSpecs>();
@@ -506,8 +508,8 @@ class MeasureHelper {
int measuredWidth;
int measuredHeight;
private int columnStarValue;
private int rowStarValue;
private float columnStarValue;
private float rowStarValue;
private boolean fakeRowAdded = false;
private boolean fakeColumnAdded = false;
@@ -681,7 +683,7 @@ class MeasureHelper {
}
private void fixColumns() {
int currentColumnWidth = 0;
float currentColumnWidth = 0;
int columnStarCount = 0;
int columnCount = this.columns.size();
@@ -706,7 +708,7 @@ class MeasureHelper {
}
private void fixRows() {
int currentRowHeight = 0;
float currentRowHeight = 0;
int rowStarCount = 0;
int rowCount = this.rows.size();
@@ -955,7 +957,7 @@ class MeasureHelper {
}
}
int measureWidth = columnsWidth + measureSpec.starColumnsCount * this.columnStarValue;
int measureWidth = (int)(columnsWidth + measureSpec.starColumnsCount * this.columnStarValue);
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measureWidth, this.stretchedHorizontally ? MeasureSpec.EXACTLY : MeasureSpec.AT_MOST);
int heightMeasureSpec = (measureSpec.autoRowsCount > 0) ? infinity : MeasureSpec.makeMeasureSpec(measureSpec.pixelHeight, MeasureSpec.EXACTLY);
@@ -1020,7 +1022,7 @@ class MeasureHelper {
}
}
int measureHeight = rowsHeight + measureSpec.starRowsCount * this.rowStarValue;
int measureHeight = (int)(rowsHeight + measureSpec.starRowsCount * this.rowStarValue);
int widthMeasureSpec = (measureSpec.autoColumnsCount > 0) ? infinity : MeasureSpec.makeMeasureSpec(measureSpec.pixelWidth, MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(measureHeight, this.stretchedVertically ? MeasureSpec.EXACTLY : MeasureSpec.AT_MOST);
@@ -1084,7 +1086,7 @@ class MeasureHelper {
ItemGroup columnGroup;
ItemGroup rowGroup;
int columnsWidth = 0;
float columnsWidth = 0;
for (int i = columnIndex; i < columnSpanEnd; i++) {
columnGroup = this.columns.get(i);
if (!columnGroup.getIsStar()) {
@@ -1092,7 +1094,7 @@ class MeasureHelper {
}
}
int rowsHeight = 0;
float rowsHeight = 0;
for (int i = rowIndex; i < rowSpanEnd; i++) {
rowGroup = this.rows.get(i);
if (!rowGroup.getIsStar()) {
@@ -1100,8 +1102,8 @@ class MeasureHelper {
}
}
int measureWidth = columnsWidth + measureSpec.starColumnsCount * this.columnStarValue;
int measureHeight = rowsHeight + measureSpec.starRowsCount * this.rowStarValue;
int measureWidth = (int)(columnsWidth + measureSpec.starColumnsCount * this.columnStarValue);
int measureHeight = (int)(rowsHeight + measureSpec.starRowsCount * this.rowStarValue);
// if (have stars) & (not stretch) - at most
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measureWidth,