简体   繁体   English

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

[英]How to center text vertically in a TextView with Java code?

I know this question has been asked many times on SO. 我知道这个问题已经被问过很多次了。 I have read most of them, but they don't work for me, so don't bother marking duplicates. 我已经阅读了其中的大多数内容,但是它们对我不起作用,因此不要打扰标记重复项。

Here is my code, and what I have tried so far: 这是我的代码,到目前为止我已经尝试过:

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);

The character is horizontally centered, but it floats on the top of the TextView vertically. 字符水平居中,但垂直漂浮在TextView的顶部。 Setting gravity and LayoutParams doesn't change its behavior. 设置gravityLayoutParams不会更改其行为。

What should I do to make it center vertically? 我应该怎么做才能使其垂直居中?

use this class for VerticalTextView. 将此类用于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();
}
}

you can do it like this 你可以这样

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

where R.layout.my_layout_file contains textview with gravity center. 其中R.layout.my_layout_file包含具有重心的textview。 and here you can get object of textview like this 在这里你可以得到像这样的textview对象

textviewObject = (TextView)view.findViewById(R.id.textViewId) 

Try setting RelativeLayout.LayoutParams to you RelativeLayout 尝试将RelativeLayout.LayoutParams设置为您RelativeLayout

RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
container.setLayoutParams(relativeLayoutParams)

Then set the gravity for your TextView: 然后为您的TextView设置重力:

tv.setGravity(Gravity.CENTER);

Then add the view to your RelativeLayout: 然后将视图添加到您的RelativeLayout中:

container.addView(tv);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM