简体   繁体   English

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

[英]Center Gravity for TextView doesn't work on xxhdpi in RelativeLayout

I have TextViews with square background with numbers that appear every second. 我有方形背景的TextViews,其数字每秒出现一次。 I use a square image for background and set the text gravity center to center the numbers in the squares. 我使用正方形图像作为背景,并将文本重心设置为使数字居中。 It doesn't work on xxhdpi phones. 在xxhdpi手机上不起作用。 How I can center the numbers? 如何将数字居中?

在此处输入图片说明

This is the code: 这是代码:

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

Try to remove 尝试删除

displayingLayoutParam.setMargins

and add 并添加

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

if this problem only appear on xxhdpi device.. than you can check it runtime for device and set marginbottom to text like this. 如果此问题仅出现在xxhdpi设备上,则可以检查设备的运行时并将marginbottom设置为marginbottom文本。 i am adding margin to my toolbar text. 我在我的工具栏文本中添加了边距。 hope you will get this trick 希望你能得到这个把戏

switch (getResources().getDisplayMetrics().densityDpi) {
            case DisplayMetrics.DENSITY_HIGH:
                Log.d("density:", "high");
                toolbar.setTitleMarginStart(160);
                break;
            case DisplayMetrics.DENSITY_XHIGH:
                // ...
                Log.d("density:", "xhigh");
                toolbar.setTitleMarginStart(200);
                break;
            case DisplayMetrics.DENSITY_XXHIGH:
                Log.d("density:", "xxhigh");
                toolbar.setTitleMarginStart(380);
                break;
        }

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

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