So I have a setup where I'm creating my own View and I'm adding some TextViews into it. However, the gravity setting is broken for it. (It centers horizontally, but not vertically) I'm doing it this way because there's other stuff I'm also drawing within my view besides just the TextViews, but those work fine. There's only a problem with the TextView gravity. Here's partial code of what I have.
public class myView extends View {
protected RelativeLayout baseLayout;
protected TextView textView1;
protected TextView textView2;
public myView (Context context) {
super(context);
setLayoutParams(new LayoutParams(FILL_PARENT, FILL_PARENT));
baseLayout = new RelativeLayout(context);
baseLayout.setLayoutParams(new LayoutParams(FILL_PARENT, FILL_PARENT));
textView1 = new TextView(context);
// initialize textView1 string, id, textsize, and color here
textView2 = new TextView(context);
// initialize textView2 string, id, textsize, and color here
baseLayout.addView(textView1);
baseLayout.addView(textView2);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Resources res = getResources();
// calculate out size and position of both textViews here
textView1.layout(left1, top1, left1 + width1, top1 + height1);
textView1.setGravity(Gravity.CENTER);
textView1.setBackgroundColor(green); // just to make sure it's drawn in the right spot
textView2.layout(left2, top2, left2 + width2, top2 + height2);
textView2.setGravity(Gravity.CENTER);
textView2.setBackgroundColor(blue); // same as above
baseLayout.draw(canvas);
}
}
This draws the TextViews in the exact spots and sizes that I want them (I know because of the background color), but the gravity sets them to be only centered horizontally.. not vertically. (yes, the TextViews are larger than the actual text strings)
I could PROBABLY implement the solution found here (TextView gravity), but that doesn't seem like a very efficient or reliable way to get around this. Is there something I'm doing wrong that's causing gravity to stop working correctly? Any input/help is appreciated.