【问题标题】:Text in TextView drawed out of 9patch padding bordersTextView 中的文本从 9patch 填充边框中绘制出来
【发布时间】:2013-11-25 00:54:57
【问题描述】:

我需要获得 TextView 与背景图像(9patch)的恒定纵横比。所以我在我的活动中使用了以下代码:

@Override
public void onResume() {
    super.onResume();

    // adding contact image resize callback for adjusting image height
    findViewById(R.id.contactText).getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            // aspect ratio
            float resizeRatio = 1.13f;

            // getting contact frame
            TextView contactView = (TextView) findViewById(R.id.contactText);
            ViewGroup.LayoutParams layout = contactView.getLayoutParams();

            // resizing contact text
            layout.height = (int) (contactView.getWidth() * resizeRatio);
            contactView.setLayoutParams(layout);
            contactView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    });
}

此代码符合我的预期 - TextView 已调整大小。但是九个补丁中描述的文本边框(填充)来自未调整大小的图像。因此文本显示在 TextView 的中心而不是底部,即填充边框所在的位置。

如何解决这个问题?

【问题讨论】:

    标签: android textview nine-patch


    【解决方案1】:

    问题是因为即使在拉伸之后,图像的顶部填充也是不变的。当android垂直拉伸图像时,它会保持顶部填充值。因此,如果填充区域不在拉伸区域中,它会增加,尽管它根本没有拉伸。

    这个问题描述here

    【讨论】:

      猜你喜欢
      • 2013-10-22
      • 1970-01-01
      • 2012-11-12
      • 1970-01-01
      • 2015-01-12
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多