scroll - 在Android上使TextView可滚动
我在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);
25个解决方案
1481 votes
实际上您不需要使用yourTextView.setMovementMethod(new ScrollingMovementMethod());。
只需设置
android:scrollbars = "vertical"
布局的xml文件中yourTextView.setMovementMethod(new ScrollingMovementMethod());的属性。
然后使用:
yourTextView.setMovementMethod(new ScrollingMovementMethod());
在你的代码中。
宾果,滚动!
Amit Chintawar answered 2019-01-15T13:39:46Z
279 votes
这就是我纯粹用XML做的方式:
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:id="@+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">
android:id="@+id/TEXT_STATUS_ID"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"/>
笔记:
android:layout_height="fill_parent"与android:layout_weight="1.0"相结合将使textview占用所有可用空间。
定义Scrollview时,请勿指定android:layout_height="fill_parent"否则滚动视图不起作用! (这导致我现在浪费了一个小时!FFS)。
专家提示:
要在添加文本后以编程方式滚动到底部,请使用以下命令:
mTextStatus = (TextView) findViewById(R.id.TEXT_STATUS_ID);
mScrollView = (ScrollView) findViewById(R.id.SCROLLER_ID);
private void scrollToBottom()
{
mScrollView.post(new Runnable()
{
public void run()
{
mScrollView.smoothScrollTo(0, mTextStatus.getBottom());
}
});
}
Someone Somewhere answered 2019-01-15T13:40:46Z
119 votes
所有真正必要的是setMovementMethod()。 这是使用LinearLayout的示例。
文件main.xml
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"
/>
文件WordExtractTes