TextView textView = (TextView) findViewById(R.id.text); textView.setMovementMethod(new ScrollingMovementMethod()); final int lineNumber = 20 textView.post(new Runnable() { @Override public void run() { int y = textView.getLayout().getLineTop(lineNumber); textView.scrollTo(0, y); } });
@Override public void run() { if(tvLog.getText().length() > 2000) { tvLog.setText(""); } tvLog.setText(tvLog.getText() + "\ninfo: " + event.msg + "\n"); int offset=tvLog.getLineCount()*tvLog.getLineHeight(); if(offset>tvLog.getHeight()){ tvLog.scrollTo(0,offset-tvLog.getHeight()); } } });
private void scrollLog(String message) { Spannable colorMessage = new SpannableString(message + "\n"); colorMessage.setSpan(new ForegroundColorSpan(0xff0000ff), 0, message.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); mShowText.append(colorMessage); Layout layout = mShowText.getLayout(); if (layout != null) { int scrollAmount = layout.getLineTop(mShowText.getLineCount()) - mShowText.getHeight(); if (scrollAmount > 0) { mShowText.scrollTo(0, scrollAmount + mShowText.getCompoundPaddingBottom()); } else { mShowText.scrollTo(0, 0); } } }
@Override public void handleMessage(Message msg) { super.handleMessage(msg); int what = msg.what; if (what == 0) { tvLog.append((String) msg.obj); int offset = tvLog.getLineCount() * tvLog.getLineHeight(); if (offset - tvLog.getHeight() > 0) { tvLog.scrollTo(0, offset - tvLog.getHeight()); } } else if (what == 1) { List<String> dumpeds = (List<String>) msg.obj; for (int i = 0; i < dumpeds.size(); i++) { String dumped = dumpeds.get(i); dumped = dumped.replaceAll(" ", ""); tvResult.append(">>[" + (i+1) + "]<<" + dumped + "\n\n"); } } } };
@Override public void handleMessage(Message msg) { switch (msg.what) { case FLAG_PLAYER_LOG: simpleActivity.tvPlayerLogArea.setText(msg.obj.toString()); int offset1 = simpleActivity.tvPlayerLogArea.getLineCount() * simpleActivity.tvPlayerLogArea.getLineHeight(); if (offset1 > simpleActivity.tvPlayerLogArea.getHeight()) { simpleActivity.tvPlayerLogArea.scrollTo(0, offset1 - simpleActivity.tvPlayerLogArea.getHeight()); } break; case FLAG_THUMB_LOG: simpleActivity.tvThumbLogArea.setText(msg.obj.toString()); int offset2 = simpleActivity.tvThumbLogArea.getLineCount() * simpleActivity.tvThumbLogArea.getLineHeight(); if (offset2 > simpleActivity.tvThumbLogArea.getHeight()) { simpleActivity.tvThumbLogArea.scrollTo(0, offset2 - simpleActivity.tvThumbLogArea.getHeight()); } break; } } };
tv.scrollTo(0, scrollY);
/** * Show the end of the URL rather than the beginning. */ @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { Layout layout = mUrlBar.getLayout(); if (layout == null) return; // Android doesn't account for the compound Drawable in its width calculations, leading to // improper scrolling and even Android improperly placing the horizontal fade in its // TextView calculation. Get around it by calculating that width manually: crbug.com/303908 int urlBarWidth = mUrlBar.getWidth(); int iconWidth = mCurrentIconResource == 0 ? 0 : mIconResourceWidths.get(mCurrentIconResource); int availableTextWidth = urlBarWidth - iconWidth; int desiredWidth = (int) Layout.getDesiredWidth(layout.getText(), layout.getPaint()); if (desiredWidth > availableTextWidth) { mUrlBar.scrollTo(desiredWidth - availableTextWidth, 0); } else { mUrlBar.scrollTo(0, 0); } }
textView.scrollTo(0, 0); return true;