在android上面讓TextView 過多的文字實現有滾動條,之前想簡單了以為設置TextView的屬性就可以實現,結果還是需要ScrollView配合使用,才能達到滾動條的效果有兩種方式實現,
一種是代碼寫java的layout:
01.RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(80,80); 02.//初始化滾動條控件 03.ScrollView scrollView =new ScrollView(this); 04.scrollView.setScrollContainer(true); 05.scrollView.setFocusable(true); 06.//初始化文字控件 07.TextView textView = new TextView(this); 08.textView.setHorizontallyScrolling(true); 09.textView.setText("哈哈哈,\n哈哈哈"); 10. 11.scrollView.addView(textView); 12.main_view.addView(scrollView, param);
另一種則用layout.xml來實現:
01.<ScrollView 02. android:layout_width="fill_parent" 03. android:layout_height="fill_parent" 04. android:scrollbars="vertical" 05. android:fadingEdge="vertical"> 06. 07. <TextView 08. android:id="@+id/textView" 09. android:layout_width="wrap_content" 10. android:layout_height="wrap_content" 11. android:text="Demo" 12. android:textSize="15sp"/> 13. </ScrollView>