You can set gravity programmatically with setGravity(int))
Perhaps you can install layout_gravityas follows (I have not tested this yet):
TextView can tell parents about layout settings with
setLayoutParams(ViewGroup.LayoutParams params)
LayoutParams params=new LayoutParams(this, attrSet);
tv.setLayoutParams(params);