Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

android - 使TextView在Android上可滚动(Making TextView scrollable on Android)

I am displaying text in a textview that appears to be too long to fit into one screen.

(我在textview中显示的文本似乎太长,无法容纳在一个屏幕中。)

I need to make my TextView scrollable.

(我需要使TextView可滚动。)

How can I do that?

(我怎样才能做到这一点?)

Here is the code:

(这是代码:)

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);
  ask by Muhammad Maqsoodur Rehman translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need to use a ScrollView actually.

(您实际上不需要使用ScrollView 。)

Just set the

(只需设置)

android:scrollbars = "vertical"

properties of your TextView in your layout's xml file.

(布局的xml文件中TextView属性。)

Then use:

(然后使用:)

yourTextView.setMovementMethod(new ScrollingMovementMethod());

in your code.

(在您的代码中。)

Bingo, it scrolls!

(宾果游戏,滚动!)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...