下面列出了android.widget.TextView#getLineCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
private boolean expand() {
if (!isCompact)
return false;
else {
TextView subtitle = (TextView) layout.findViewById(R.id.notification_subtitle);
if ( (subtitle.getLineCount() <= MIN_LINES && subtitle.length() < 80) && !isActionButtons) {
return false;
}
isCompact = false;
subtitle.setMaxLines(MAX_LINES);
if (isActionButtons)
themeClass.showActionButtons(layout, -1);
if (displayTime < MAX_DISPLAY_TIME) {
handler.removeCallbacks(closeTimer);
handler.postDelayed(closeTimer, displayTime);
}
return true;
}
}
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;
}
/**
* 获取文本实际所占高度,辅助用于计算行高,行数
*
* @param text 文本
* @param textSize 字体大小
* @param deviceWidth 屏幕宽度
*/
private void measureTextViewHeight(String text, float textSize, int deviceWidth) {
TextView textView = new TextView(getContext());
textView.setText(text);
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(deviceWidth, MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
textView.measure(widthMeasureSpec, heightMeasureSpec);
originalLineCount = textView.getLineCount();
originalHeight = textView.getMeasuredHeight();
}
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());
}
}
/**
* Returns a {@link Runnable} that sets up toggleable scrolling behavior on a {@link TextView}
* if the number of lines to be displayed exceeds the maximum lines limit supported by the TextView.
* Can be applied by using {@link View#post(Runnable)}.
*
* @param textView TextView that the Runnable should apply against
* @return Runnable
*/
public static Runnable getMarqueeToggleableAction(final TextView textView) {
return new Runnable() {
@Override
public void run() {
int lines = textView.getLineCount();
int maxLines = TextViewCompat.getMaxLines(textView);
if (lines > maxLines) {
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setClickable(true);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setSelected(!v.isSelected());
TextUtils.TruncateAt ellipsize;
if (v.isSelected()) {
ellipsize = TextUtils.TruncateAt.MARQUEE;
} else {
ellipsize = TextUtils.TruncateAt.END;
}
textView.setEllipsize(ellipsize);
textView.setHorizontallyScrolling(v.isSelected());
}
});
}
}
};
}
/**
* 获取文本实际所占高度,辅助用于计算行高,行数
*
* @param text 文本
* @param textSize 字体大小
* @param deviceWidth 屏幕宽度
*/
private void measureTextViewHeight(String text, float textSize, int deviceWidth) {
TextView textView = new TextView(getContext());
textView.setText(text);
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(deviceWidth, MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
textView.measure(widthMeasureSpec, heightMeasureSpec);
originalLineCount = textView.getLineCount();
originalHeight = textView.getMeasuredHeight();
}
/**
* 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;
}