0
我使用scrollview中的textview創建了可運行的打字機效果。在Android中的ScrollView中使用TextView.postDelayed的打字機效果
這是我做過什麼......
final ScrollView sv = new ScrollView(this);
sv.setLayoutParams(new LinearLayout.LayoutParams((int)display.getWidth()/2, (int)((display.getHeight()/4))*3));
sv.setPadding(0, px2DP(10), px2DP(10), px2DP(10));
final CharSequence text = getResources().getText(R.string.longstring);
final TextView content = new TextView(this);
content.setBackgroundColor(getResources().getColor(R.color.Black));
content.setTextColor(getResources().getColor(R.color.White));
content.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
content.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
sv.addView(content);
content.postDelayed(new Runnable(){
@Override
public void run() {
content.append(text.subSequence(0, index++));
sv.fullScroll(View.FOCUS_DOWN);
}
},70);
我只得到一個黑盒子,並沒有文字。
我在做什麼錯?請幫忙。
PS。我已經找到了解決方案!
您是否將ScrollView添加到主視圖?另外,如果你在Runnable中執行'sv.invalidate()',該怎麼辦? – Jonas
是的,測試以確保一切安裝正確。嘗試在content.postDelayed(...);':content.setText(「Testing」)之前添加,以確保簡單案例正確顯示。 – kabuko
另請注意,postDelayed只運行一次Runnable。 http://developer.android.com/reference/android/view/View.html#postDelayed(java.lang.Runnable,+long) – Jonas