在以编程方式创建TextView后,我需要帮助找出TextView的高度。我生成一个像这样的TextView:
public TextView drawTextView(String text, boolean center, boolean bold, int topMargin, int leftMargin, int textSize) {
View vt = new TextView(getBaseContext());
final TextView textView = new AutoResizeTextView(vt.getContext());
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/GothamMedium.ttf");
textView.setText(text);
textView.setTextColor(0xFFFFFFFF);
if (bold) {
textView.setTypeface(tf, Typeface.BOLD);
} else {
textView.setTypeface(tf);
}
if (center) {
textView.setGravity(Gravity.CENTER);
}
textView.setTextSize(textSize);
textView.setSingleLine(false);
LayoutParams paramsText = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsText.leftMargin = leftMargin;
paramsText.topMargin = topMargin;
container.addView(textView, paramsText);
return textView;
}
我希望能做的事情是这样的:
TextView text = drawTextView(map.get("business"), false, true, topMargin, 30, 40);
topMargin += text.getHeight() + 5;
text = drawTextView(map.get("text"), false, true, topMargin, 30, 16);
topMargin += text.getHeight() + 5;
text = drawTextView(map.get("detail"), false, false, topMargin, 30, 16);
topMargin += text.getHeight() + 5;
因此,我知道来自不同TextView的文本不会重叠,即使文本最终比一行文本更高,也总会有5个像素的差异,并且为了记帐针对不同手机上的不同TextView尺寸。但是text.getHeight()总是只返回0.我能做些什么来解决这个问题,我一直在寻找解决方案,但我还没有找到解决方案。
答案 0 :(得分:0)
我建议使用垂直LinearLayout,而不是手动绘制绝对位置。您可以将每个TextView的上边距设置为5像素。它可能看起来像这样:
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.textContainer); //Or make one programmatically
TextView textView = ...; //Your creation code from above plus layout params with 5 pixel top margin
linearLayout.addView(textView);
答案 1 :(得分:0)
你可以做到
yourView.getLayoutParams().height