我有方形背景的TextViews,其数字每秒出现一次。我使用正方形图像作为背景,并将文本重心设置为使数字居中于正方形。在xxhdpi手机上不起作用。如何将数字居中?

java - TextView的Center Gravity在RelativeLayout中的xxhdpi上不起作用-LMLPHP

这是代码:

displayingCell = new TextView(MyActivity.this);
displayingCell.setTypeface(null, Typeface.BOLD);
displayingCell.setTextSize(mySize);
displayingCell.setTextColor(Color.BLACK);
displayingCell.setGravity(Gravity.CENTER);
displayingLayoutParam = new RelativeLayout.LayoutParams(diameter, diameter);
displayingLayoutParam.setMargins(marginLeft, marginTop, 0, 0);
displayingCell.setLayoutParams(displayingLayoutParam);
displayingCell.setBackground(myImage);
displayingCell.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

最佳答案

尝试删除

displayingLayoutParam.setMargins


并添加

displayingLayoutParam.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);

10-04 10:48