티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

Making TextView scrollable on Android

Android에서 TextView 스크롤 가능하게 만들기

 문제 내용 

I am displaying text in a TextView that appears to be too long to fit into one screen. I need to make my TextView scrollable. 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);

 

 

 높은 점수를 받은 Solution 

You don't need to use a ScrollView actually.

실제로 스크롤 뷰를 사용할 필요는 없습니다.

 

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!

빙고, 스크롤한다!

 

 

 

 가장 최근 달린 Solution 

I know this is an older post, but this is how I am handling the issue on the Java side.

나는 이것이 오래된 게시물이라는 것을 알지만, 이것이 내가 자바 쪽에서 그 문제를 다루는 방법이다.

 

    // Allow textView to scroll
    tv.setSingleLine(true);
    tv.setHorizontallyScrolling(true);
    tv.setEllipsize(TextUtils.TruncateAt.MARQUEE);
    tv.setMarqueeRepeatLimit(-1); // Infinite
    // TextView must be 'selected'
    tv.setSelected(true);
    // Padding not necessary, but this helps so the text isn't right
    // up against the side of a screen/layout
    tv.setPadding(10, 0, 10, 0);

 

 

출처 : https://stackoverflow.com/questions/1748977/making-textview-scrollable-on-android

반응형
댓글
공지사항
최근에 올라온 글