【问题标题】:How to center text vertically in a TextView with Java code?如何使用 Java 代码在 TextView 中垂直居中文本?
【发布时间】:2018-04-03 17:05:18
【问题描述】:

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

这是我的代码,以及迄今为止我尝试过的:

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 的顶部。设置 gravityLayoutParams 不会改变它的行为。

我应该怎么做才能使它垂直居中?

【问题讨论】:

  • 你试过把它的宽高设为wrap_content吗?
  • 设置布局参数并设置高度以匹配父级.. 然后 center_vertical 将起作用
  • @R.R.M 我已经尝试过wrap_contentmatch_parent
  • 好的,然后尝试将 relativeLayout 的参数设置为 match_parent

标签: java android textview alignment


【解决方案1】:

将此类用于 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();
}
}

【讨论】:

    【解决方案2】:

    你可以这样做

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

    其中 R.layout.my_layout_file 包含带有重心的文本视图。在这里你可以像这样得到textview的对象

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

    【讨论】:

      【解决方案3】:

      尝试将 RelativeLayout.LayoutParams 设置为您的 RelativeLayout

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-14
        • 2023-03-16
        • 1970-01-01
        • 2020-09-01
        • 1970-01-01
        • 2013-10-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多