下面列出了android.widget.TextView#getLineHeight ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public TinyCoach(Application context, FPSConfig config) {
fpsConfig = config;
//create meter view
meterView = (TextView) LayoutInflater.from(context).inflate(R.layout.meter_view, null);
//set initial fps value....might change...
meterView.setText((int) fpsConfig.refreshRate + "");
// grab window manager and add view to the window
windowManager = (WindowManager) meterView.getContext().getSystemService(Service.WINDOW_SERVICE);
int minWidth = meterView.getLineHeight()
+ meterView.getTotalPaddingTop()
+ meterView.getTotalPaddingBottom()
+ (int) meterView.getPaint().getFontMetrics().bottom;
meterView.setMinWidth(minWidth);
addViewToWindow(meterView);
}
private void adjustTextViewHeight() {
View view = getView();
if (view == null) {
return;
}
TextSwitcher textSwitcher = (TextSwitcher) view;
TextView curr = (TextView) textSwitcher.getCurrentView();
TextView next = (TextView) textSwitcher.getNextView();
int currH = curr.getLineCount() * curr.getLineHeight();
int nextH = next.getLineCount() * next.getLineHeight();
if (currH != nextH) {
curr.setHeight(currH);
next.setHeight(currH);
}
}
public int getTopVisibleLine(TextView editor) {
int lineHeight = editor.getLineHeight();
if (lineHeight == 0) {
return 0;
}
int line = editor.getScrollY() / lineHeight;
if (line < 0) {
return 0;
}
if (line >= editor.getLineCount()) {
return editor.getLineCount() - 1;
}
return line;
}
public int getBottomVisibleLine(TextView editor) {
int lineHeight = editor.getLineHeight();
if (lineHeight == 0) {
return 0;
}
int line = Math.abs((editor.getScrollY() + editor.getHeight()) / lineHeight) + 1;
if (line < 0) {
return 0;
}
if (line >= editor.getLineCount()) {
return editor.getLineCount() - 1;
}
return line;
}
private void configureViewForPosition(View itemView, int position) {
TextView dateText = (TextView) itemView.findViewById(R.id.push_date);
TextView titleText = (TextView) itemView.findViewById(R.id.push_title);
TextView descriptionText = (TextView) itemView.findViewById(R.id.push_description);
ImageView iconImage = (ImageView) itemView.findViewById(R.id.push_icon_image);
String title = entries.get(position).getTitle();
if (title.equals(""))
title = entries.get(position).getService().getName();
String description = entries.get(position).getMessage();
Date pushDate = entries.get(position).getLocalTimestamp();
Bitmap icon = entries.get(position).getService().getIconBitmapOrDefault(context);
dateText.setText(this.dateFormat.format(pushDate));
titleText.setText(title);
descriptionText.setText(description);
iconImage.setImageBitmap(icon);
TypedValue value = new TypedValue();
DisplayMetrics metrics = new DisplayMetrics();
context.getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, value, true);
((WindowManager) (context.getSystemService(Context.WINDOW_SERVICE))).getDefaultDisplay().getMetrics(metrics);
int lineCount = selectedIndex == position ? descriptionText.getLineCount() : 0;
int minHeight = (int) TypedValue.complexToDimension(value.data, metrics);
int prefHeight = (lineCount + 2) * descriptionText.getLineHeight();
itemView.getLayoutParams().height = prefHeight > minHeight ? prefHeight : minHeight;
}
private void addText(TextView textView, String content) {
textView.append(content);
textView.append("\n");
int offset = textView.getLineCount() * textView.getLineHeight();
if (offset > textView.getHeight()) {
textView.scrollTo(0, offset - textView.getHeight());
}
}
/**
* 改变外框大小,适应文字
* 如果当前文字只有一行,则缩到外框适应文字大小
* 如果文字有多行,则宽度不变,缩小高度到适应文字大小
* 文字外框的位置不变
*
* @param view
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void adjustSize(TextView view) {
if (view != null && view.getLayoutParams() != null) {
/*
//更好的实现方式,但是ios那边不支持这种方式
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
view.setLayoutParams(layoutParams);*/
// 废弃的实现方式,使用WRAP_CONTENT最简单,且这种方式算出来的width有bug
// @Deprecated
/*int lineCount = Math.min(view.getLineCount(), getMaxLines(view));
int width, height;
if (lineCount > 1) {//多行,宽度不变,改变高度
width = view.getWidth();
height = view.getLineHeight() * lineCount + view.getPaddingTop() + view.getPaddingBottom();
} else {//一行,改变宽度&高度
Paint paint = view.getPaint();
width = (int) paint.measureText(view.getText().toString()) + view.getPaddingLeft() + view.getPaddingRight();
height = view.getLineHeight() + view.getPaddingTop() + view.getPaddingBottom();
}*/
int lineCount = Math.min(view.getLineCount(), getMaxLines(view));
float width, height;
if (lineCount > 1) {//多行,宽度不变,改变高度
width = view.getWidth();
height = view.getLineHeight() * lineCount + view.getPaddingTop() + view.getPaddingBottom();
} else {//一行,改变宽度&高度
width = view.getPaddingLeft() + Layout.getDesiredWidth(view.getText(), view.getPaint()) + view.getPaddingRight();
height = view.getLineHeight() + view.getPaddingTop() + view.getPaddingBottom();
}
if (view.getLayoutParams() != null) {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = (int) width;
layoutParams.height = (int) height;
view.setLayoutParams(layoutParams);
}
}
}
/**
* Returns the standard height (i.e. without text effects such as shadow) of all the text of
* the given {@link TextView}.
* @param textView The {@link TextView} to determine the height of all its text content.
* @return The standard height of all the text content of the given {@link TextView}.
*/
private int heightOf(final TextView textView) {
return textView.getLineCount() > 0 ? textView.getLineHeight() * textView.getLineCount() : 0;
}