android textview 多行滚动,scroll - 在Android上使TextView可滚动

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值