簡體   English   中英

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

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

我有方形背景的TextViews,其數字每秒出現一次。 我使用正方形圖像作為背景,並將文本重心設置為使數字居中。 在xxhdpi手機上不起作用。 如何將數字居中?

在此處輸入圖片說明

這是代碼:

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

如果此問題僅出現在xxhdpi設備上,則可以檢查設備的運行時並將marginbottom設置為marginbottom文本。 我在我的工具欄文本中添加了邊距。 希望你能得到這個把戲

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