Gravity of a child TextView does not work with custom ViewGroup

I have the following classes:

public class MyGridView extends ViewGroup {
...
@Override
public void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {

super.onSizeChanged(xNew, yNew, xOld, yOld);
// do calculations for drawing bounds of child views
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    View child;
Rect rect;

for (int i=0; i < getChildCount(); i++) {
    child = getChildAt(i);

    rect = getBoundsAtChildIndex(i)
        child.layout(rect.left, rect.top, rect.right, rect.bottom);
    }
}
...
}

AND

public class MyAdapter {

public void setMyGridView(MyGridView myGridView) {

// add a single TextView to my grid view
textView = createTextView(context);
addTextViewToGrid(textView);
container.add(textView);
}

private TextView createTextView(Context context) {
    TextView textView = new TextView(context);

    textView.setText("1");
    textView.setTextColor(0xffffffff);

    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f);
    textView.setTypeface(typeface);

    textView.setGravity(Gravity.CENTER | Gravity.FILL);
// textView.setGravity(Gravity.RIGHT); // other tries to see gravity
// textView.setGravity(Gravity.FILL);

    return textView;
}

private void addTextViewToGrid(TextView textView) {
    myGridViewPtr.addView(textView, ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);
}
...
}

I have created a custom grid view and adapter, where its children draw to the screen to the correct size and location. However, their gravity, set in the createTextView() is not observed.

I can set a background image to the textView and see it fill its space in the grid.

In contrast, the text in textView always draws in its top left corner, and it always stays at the text size I set it at 12sp, rather than scale to fit using Gravity.FILL.

Preferably, the text would scale to fit and center within the textview.

EDIT

I have added the following method for onMeasure() in ViewGroup:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int width = getMeasuredWidth();
    int height = getMeasuredHeight();

    doUpdateOnSizeChanged(width, height);

    for (int i = 0; i < getChildCount(); i++)
        getChildAt(i).measure((int) viewDim.x, (int) viewDim.y);

    setMeasuredDimension(width, height);
}

The children are supposed to be the same height, width as each other. The android example code for ViewGroup is more sophisticated than I require. For instance, I am not getting the max of all children width, height.

For my onMeasure(), the child width, height in ViewDim is width, height integers that are computed in doUpdateOnSizeChanged to be less than getMeasuredWidth, getMeasuredHeight.

The result is now text aligns in the bottom-left corner of the TextView.


onMeasure需要较小的修正,基本上需要依靠MeasureSpec:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int width, height;
    int cWidth1, cHeight1;

    width = MeasureSpec.getSize(widthMeasureSpec);
    height = MeasureSpec.getSize(heightMeasureSpec);

    doUpdateOnSizeChanged(width, height);


    cWidth1 = (int)viewDim.x; cHeight1 = (int)viewDim.y;

    measureChildren(MeasureSpec.makeMeasureSpec(cWidth1, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(cHeight1, MeasureSpec.EXACTLY));

    setMeasuredDimension(width, height);
}
链接地址: http://www.djcxy.com/p/84756.html

上一篇: jQuery更改事件在IE11中不起作用

下一篇: 子级TextView的重力不适用于自定义ViewGroup