2017-11-10 6 views
-2

텍스트를 설정해야하는 TextView가 있습니다. 하지만 일부 텍스트를 그것에 추가하는 중입니다. 연결된 텍스트 만 색을 얻을 수는 있지만 전체 텍스트는 그렇지 않습니다. storyLine=storyLine.substring(0,190)+" ...Click To Expand";. 여기서 storyLine은 텍스트 뷰에서 설정 될 마지막 텍스트입니다. 나는 단지 "Click To Expand"의 색깔을 바꾸고 싶다.텍스트의 색을 설정하는 방법 연결하기

+1

[TextView 내에 여러 스타일을 포함 할 수 있습니까?] (https://stackoverflow.com/questions/1529068/is-it-possible-to-have-multiple-styles-inside-a- 텍스트보기) – Jordan

답변

0
String cntStr = " ...Click To Expand"; 
String storyLine = storyLine.substring(0,190); 
String textLine = storyLine + cntStr; 

Spannable spannable = new SpannableString(textLine); 

spannable.setSpan(new ForegroundColorSpan(Color.RED), storyLine.length(), textLine.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

myTextView.setText(spannable, TextView.BufferType.SPANNABLE); 

해피 코딩 !!!!

+0

downvote에 대한 이유가 무엇입니까? – androidOnHigh

-1

당신은 당신이에 의해 달성 할 수

String styledText = storyLine + "<font color='blue'>Click To Expand</font>"; 
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE); 
-1

을 시도 할 수 있습니다 :

SpannableStringBuilder builder = new SpannableStringBuilder(); 
String red = "Click To Expand"; 
builder.append("storyLine=storyLine.substring(0,190)+" ..."); 
SpannableString redSpannable= new SpannableString(red); 
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0); 
builder.append(redSpannable); 
mTextView.setText(builder, BufferType.SPANNABLE); 
코딩

해피!

-1

SpannableString을 사용하여 하위 문자열의 색을 지정할 수 있습니다. 필요한 경우

SpannableString span1=new SpannableString("Hello World...Click to Expand"); 
span1.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 29, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
Textview.setText(span1,TextView.BufferType.SPANNABLE); 

또한 레이아웃 XML의 spannable

Textview.setText(TextUtils.concat(AnyString, span1)); 

설정 Texview 유형을 연결할 수 있습니다.

관련 문제