【发布时间】: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