2016-07-21 2 views
2

임의의 배경색을 가진 TextView가 있습니다 (실제로 어떤 색상이든 가능). 이 Textview에는 읽을 수 있어야하는 텍스트가 있습니다. 최선의 해결책은 상기 텍스트를 흰색으로 강조 표시하고 텍스트 색상을 검정색으로 설정하는 것입니다.텍스트 뷰 내에서 텍스트 강조 표시

제 질문은 XML에서 texview 내부의 텍스트를 강조 표시 할 수 있습니까?

<TextView 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:id="@+id/colorButton4" 
     android:layout_gravity="right|bottom" 
     android:background="@drawable/layout_border" 
     android:layout_marginRight="30dp" 
     android:layout_marginBottom ="30dp" 
     android:clickable="true" 
     android:onClick="onClick" 
     android:gravity="center" 
     android:textColorHighlight="@color/bgWhite" 
     android:textColor="@color/Black" 
     android:text="5431354" /> 

을하지만 텍스트를 강조 deosn't :

나는 내 레이아웃에 다음 있습니다. https://developer.android.com/reference/android/widget/TextView.html#attr_android:textColorHighlight

그것은에만 사용되는 텍스트를 선택할 때, 예를 들면 : 당신이 텍스트 뷰에 대한 문서를 확인했을 경우

+0

하이라이트로 무엇을 의미합니까? – finki

+1

TextView 배경색에 관계없이 텍스트를 볼 수있게하십시오. 다음과 같이하십시오 : http://i1.wp.com/texblog.org/Wordpress/wp-content/uploads/2015/05/latex-highlight-text.png – Slamit

+0

내 대답은 https : // stackoverflow보세요. com/a/47142724/2685454 – Sumit

답변

0

당신은 android:textColorHighlight 당신이 원하는 것을하지 않는 것을 발견했을 것이다 EditText에서. TextView의 배경을 설정하여 "강조 표시"해야합니다.

+0

사실 그 점을 이해합니다. 내 질문은 다른 방법으로 가능합니까? 나는 멍청 하긴하지만 멍청한 놈처럼 내가 모르는 트릭이있을 경우를 대비해서 질문한다. – Slamit

+0

어쨌든 왜 검은 색으로 "강조 표시"할 때 텍스트 뷰에서 배경을 설정했는지 이해할 수 없습니까? ;) – finki

+0

임의의 배경색과 TextView를 갖고 싶습니다. "배경색은 "입니다. 배경색은입니다. 내 텍스트를 읽을 수 있습니다. 해군은 읽기가 어렵습니다. 나는 그것을 읽을 수 없다. 제대로 설명하기 바란다. – Slamit

3

이 경우 SpannableString을 사용하면 문자열의 개별 부분을 TextView에서 다르게 렌더링 할 수 있습니다.

SpannableString str = new SpannableString("Highlighted. Not highlighted."); 
    str.setSpan(new BackgroundColorSpan(Color.YELLOW), 0, 11, 0); 
    textView.setText(str); 
+0

Java 코드에서 어딘가에 가지 않고도 할 수 없다고 말하는가? – Slamit

+0

특정 HTML 태그를 사용하여 strings.xml에 텍스트 스타일을 지정할 수 있습니다. 다음 답변 중 하나를 시도하십시오 : http://stackoverflow.com/questions/17559019/text-view-with-different-colored-texts-in-xml-code하지만 글꼴 색상 대신 배경색을 사용하십시오 – Egg

+0

링크를 제공해 주셔서 감사합니다. 몇 가지 흥미로운 점이 있습니다! 안타깝게도 XML만으로 해결되는 솔루션은 없습니다. 나는 그것을 위해 자바 코드를 사용해야 할 것 같다. – Slamit

1

사용이 방법을 특정 텍스트의 모든 항목을 강조 표시 :

과 같이

private void highlightString(String input) { 
//Get the text from text view and create a spannable string 
SpannableString spannableString = new SpannableString(mTextView.getText()); 
//Get the previous spans and remove them 
BackgroundColorSpan[] backgroundSpans = spannableString.getSpans(0, spannableString.length(), BackgroundColorSpan.class); 

for (BackgroundColorSpan span: backgroundSpans) { 
    spannableString.removeSpan(span); 
} 

//Search for all occurrences of the keyword in the string 
int indexOfKeyword = spannableString.toString().indexOf(input); 

while (indexOfKeyword > 0) { 
    //Create a background color span on the keyword 
    spannableString.setSpan(new BackgroundColorSpan(Color.YELLOW), indexOfKeyword, indexOfKeyword + input.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

    //Get the next index of the keyword 
    indexOfKeyword = spannableString.toString().indexOf(input, indexOfKeyword + input.length()); 
} 

//Set the final text on TextView 
mTextView.setText(spannableString);} 

참고 : mTextView은 텍스트 강조하고자하는 텍스트 뷰 객체입니다

+0

while while (indexOfKeyword! = -1) {...}'또한 텍스트의 첫 번째 단어를 강조 표시합니다. –