我正在显示文本的TextView,似乎太长,以适合
在一个屏幕上。我需要使我的TextView可滚动。我该怎么办
了吗?
代码如下:
final TextView tv = new TextView(this);
tv.setBackgroundResource(R.drawable.splash);
tv.setTypeface(face);
tv.setTextSize(18);
tv.setTextColor(R.color.BROWN);
tv.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL);
tv.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
Random r = new Random();
int i = r.nextInt(101);
if (e.getAction() == e.ACTION_DOWN) {
tv.setText(tips[i]);
tv.setBackgroundResource(R.drawable.inner);
}
return true;
}
});
setContentView(tv);
对于一个垂直或水平滚动的TextView,其他一些答案的帮助,但我需要能够滚动两种方式。
什么最后工作是一个ScrollView与一个HorizontalScrollView里面,和一个TextView里面的HSV。它非常光滑,可以很容易地从一边到另一边或从上到下在一个滑动。它还只允许在一个方向上滚动,所以在向上或向下滚动时不会从一边跳到另一边。
禁用编辑和光标的EditText可以工作,但感觉很糟糕。每次滚动都会移动光标,甚至在100行的文件中也需要多次滑动才能从上到下或从一边到另一边。
使用setHorizontallyScrolling(true)可以工作,但没有类似的方法来允许垂直滚动,而且据我所知,它在ScrollView内部不起作用(尽管只是学习,可能是错误的)。
这是“如何应用滚动条到你的TextView”,只使用XML。
首先,你需要在main.xml文件中获取一个Textview控件,并将一些文本写入其中…是这样的:
<TextView
android:id="@+id/TEXT"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/long_text"/>
接下来,将文本视图控件放在滚动视图之间,以显示此文本的滚动条:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<ScrollView
android:id="@+id/ScrollView01"
android:layout_height="150px"
android:layout_width="fill_parent">
<TextView
android:id="@+id/TEXT"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/long_text"/>
</ScrollView>
</RelativeLayout>
就是这样……