如何使用Java代码在TextView中垂直居中文本?

如何使用Java代码在TextView中垂直居中文本?,java,android,textview,alignment,Java,Android,Textview,Alignment,我知道这个问题已经被问过很多次了。我已经读了其中的大部分,但它们对我不起作用,所以不要麻烦标记重复的内容 这是我的代码,也是我迄今为止所尝试的: RelativeLayout container = new RelativeLayout(this.getContext()); TextView tv = new TextView(this.getContext()); tv.setText(txt); // a single digit like '3' tv.setLines(1); tv.

我知道这个问题已经被问过很多次了。我已经读了其中的大部分,但它们对我不起作用,所以不要麻烦标记重复的内容

这是我的代码,也是我迄今为止所尝试的:

RelativeLayout container = new RelativeLayout(this.getContext());

TextView tv = new TextView(this.getContext());
tv.setText(txt); // a single digit like '3'
tv.setLines(1);
tv.layout(0, offsety, cellszie, offsety+cellsize);

tv.setTextAlignment(TextView.TEXT_ALIGNMENT_CENTER);
tv.setGravity(Gravity.CENTER);

// I also tried CENTER_VERTIAL and the following line
// tv.setGravity(CENTER_VERTIAL| CENTER_HORIZONTAL);

// I also tried giving LayoutParams to tv like this:
// tv.setLayoutParams(new LayoutParams(cellsize, cellsize));
// tv.setLayoutParams(new LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
// tv.setLayoutParams(new LayoutParams(MATCH_PARENT, MATCH_PARENT));

container.addView(tv);
该字符水平居中,但垂直浮动在TextView的顶部。设置
gravity
LayoutParams
不会改变其行为


如何使其垂直居中?

将此类用于VerticalTextView

public class VerticalTextView extends TextView {
final boolean topDown;

public VerticalTextView(Context context, AttributeSet attrs){
    super(context, attrs);
    final int gravity = getGravity();
    if(Gravity.isVertical(gravity) && (gravity&Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
        setGravity((gravity&Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP);
        topDown = false;
    }else
        topDown = true;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    super.onMeasure(heightMeasureSpec, widthMeasureSpec);
    setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}

@Override
protected void onDraw(Canvas canvas){
    TextPaint textPaint = getPaint();
    textPaint.setColor(getCurrentTextColor());
    textPaint.drawableState = getDrawableState();

    canvas.save();

    if(topDown){
        canvas.translate(getWidth(), 0);
        canvas.rotate(90);
    }else {
        canvas.translate(0, getHeight());
        canvas.rotate(-90);
    }
    canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());

    getLayout().draw(canvas);
    canvas.restore();
}
}

你可以这样做

RelativeLayout container = new RelativeLayout(this.getContext());
View view = layoutInflater.inflate(R.layout.my_layout_file, null);
container.addView(view);
textviewObject = (TextView)view.findViewById(R.id.textViewId) 
其中R.layout.my_layout_文件包含带重心的文本视图。在这里,你可以像这样得到textview的对象

RelativeLayout container = new RelativeLayout(this.getContext());
View view = layoutInflater.inflate(R.layout.my_layout_file, null);
container.addView(view);
textviewObject = (TextView)view.findViewById(R.id.textViewId) 

尝试为您的RelativeLayout设置RelativeLayout.LayoutParams

RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
container.setLayoutParams(relativeLayoutParams)
然后设置TextView的重力:

tv.setGravity(Gravity.CENTER);
然后将视图添加到您的RelativeLayout:

container.addView(tv);

您是否尝试将其宽度和高度设置为包裹内容?请尝试此设置布局参数并将高度设置为与父项匹配。。然后将中心垂直work@R.R.M我已经尝试了
wrap\u content
match\u parent
。确定,然后尝试将relativeLayout的参数设置为match\u parent