SPAN_EXCLUSIVE_EXCLUSIVE와 같은 Span 플래그의 의미를 설명하십시오.
누군가가 명확하게 예제와 함께 어떤 각 설명 할 수 스팬 플래그를 좋아 SPAN_EXCLUSIVE_EXCLUSIVE
하고 SPAN_MARK_MARK
어떤 플래그를 사용하는 평균 언제?
다음과 같은 공식 문서를 이해하지 못합니다.
유형의 범위
SPAN_EXCLUSIVE_EXCLUSIVE
는 시작 또는 끝 지점에 삽입 된 텍스트를 포함하도록 확장되지 않습니다.
'포함하도록 확장'은 범위를 삽입 한 후 수정 한 내용을 의미하나요?
이 플래그가 변경 불가능한 텍스트가있는 Spannable에 영향을주지 않는다는 의미인가요?
'포함하도록 확장'은 범위를 삽입 한 후 수정 한 내용을 의미하나요?
예. 예를 들어 다음이 있다고 가정 해 보겠습니다.
빠른 여우 증가했다.
우리가 사용하는 경우 SPAN_EXCLUSIVE_EXCLUSIVE
스팬의 중간에 텍스트 삽입 우리를 굵은 범위에, 그리고 여전히 볼드체 :
빠른 갈색 여우가 증가했다.
그러나 굵은 체 범위의 시작 또는 끝에 텍스트를 삽입하면 삽입 된 텍스트는 굵게 표시되지 않습니다.
정말 빠른 여우가 뛰어 올랐다.
그러나을 사용 SPAN_INCLUSIVE_EXCLUSIVE
했다면 처음에 텍스트를 삽입하는 것이 범위의 일부로 포함되며 다음과 같이됩니다.
정말 빠른 여우가 증가했다.
이 플래그가 변경 불가능한 텍스트가있는 Spannable에 영향을주지 않는다는 의미인가요?
나는 그들이 불변 텍스트에 대해 제한된 사용을 가지고 있다고 말할 것입니다. 대부분의 경우, SpannableStringBuilder
덮개 아래 (예 :) 와 함께 사용됩니다 EditText
.
플래그가 의미하지 않는 것
플래그 의 INCLUSIVE
및 EXCLUSIVE
부분을 처음 보았을 때 Spannable
스팬에 스팬의 시작 및 끝 인덱스 위치가 포함되어 있는지 여부를 방금 알려 주었다고 생각했습니다. 이것은 사실이 아닙니다. 다음 예를 통해 설명하겠습니다.
String myString = "01234";
int start = 1;
int end = 3;
int spanFlag = Spannable.SPAN_INCLUSIVE_INCLUSIVE; // this is what is changing
SpannableString spannableString = new SpannableString(myString);
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED);
spannableString.setSpan(foregroundSpan, start, end, spanFlag);
textView.setText(spannableString);
결과는 다음과 같습니다.
SPAN_INCLUSIVE_INCLUSIVE
SPAN_INCLUSIVE_EXCLUSIVE
SPAN_EXCLUSIVE_INCLUSIVE
SPAN_EXCLUSIVE_EXCLUSIVE
그들은 모두 동일합니다! 플래그는 범위에 영향을주지 않습니다. 범위는 항상 시작 색인에 문자를 포함하고 끝 색인에있는 문자를 제외합니다.
플래그가 실제로 의미하는 것
Spannable 플래그 의 INCLUSIVE
및 EXCLUSIVE
부분은 실제로 범위 에 시작 또는 끝 위치에 삽입 된 텍스트를 포함해야하는지 여부를 알려줍니다 .
다음은이를 설명하기 위해 수정 된 예입니다.
String myString = "01234";
int start = 1;
int end = 3;
int spanFlag = Spannable.SPAN_INCLUSIVE_INCLUSIVE; // this is what is changing
// set the span
SpannableStringBuilder spannableString = new SpannableStringBuilder(myString);
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED);
spannableString.setSpan(foregroundSpan, start, end, spanFlag);
// insert the text after the span has already been set
// (inserting at start index second so that end index doesn't get messed up)
spannableString.insert(end, "x");
spannableString.insert(start, "x");
textView.setText(spannableString);
x
끝 및 시작 인덱스에를 삽입 한 후의 결과는 다음과 같습니다 .
SPAN_INCLUSIVE_INCLUSIVE
SPAN_INCLUSIVE_EXCLUSIVE
SPAN_EXCLUSIVE_INCLUSIVE
SPAN_EXCLUSIVE_EXCLUSIVE
메모
- 두 번째 예제에서는 a
SpannableStringBuilder
의 텍스트SpannableString
가 변경 불가능하여 텍스트를 삽입 할 수 없기 때문에 a를 사용해야 했습니다. 따라서 플래그는 일반적으로SpannableString
. 그러나에서SpannableString
복사 된 범위 가SpannableStringBuilder
또는에 걸쳐Editable
있고 거기에서 플래그가 의미를 갖는 상황을 상상할 수 있습니다. SpannableString
,,SpannableStringBuilder
등 의 차이점 은이 답변 을 참조하십시오Editable
.
res / values 디렉토리의 xml 파일 내에서 리소스로 유지하는 문자열 값은 android.text.Spannable 인터페이스에 정의 된 메서드, android.text.Spanned 인터페이스에 정의 된 필드 (Spannable은 하위 인터페이스)로 스타일을 지정할 수 있습니다. of Spanned), android.text.SpannableStringBuilder 클래스 (SpannableStringBuilder는 Spannable의 하위 클래스 임)와 android.text.style 패키지, 특히 android.text.style.ForegroundColorSpan 및 android.text.style.StyleSpan에 정의 된 클래스 클래스. SpannableStringBuilder는 Spannable의 하위 클래스이고 Spannable은 Spanned의 하위 인터페이스이기 때문에 Spanned 인터페이스의 모든 필드와 Spannable 인터페이스의 메서드의 완전한 구현은 SpannableStringBuilder 클래스에 존재합니다.
1. 이제 텍스트 리소스의 스타일을 지정하려면 먼저 Resources 개체에서 getString (int resource_id) 메서드를 사용하여 리소스 ID를 가져와야하며, Context 하위 클래스와 연결된 Resources 개체를 가져 오려면 getResources () 메서드를 호출합니다. 예-
문자열 제목 = getResources (). getString (R.string.string_title);
2. 이제 공개 매개 변수 없음 생성자 또는 다른 공개 생성자로 SpannableStringBuilder 객체를 만들어야합니다.
3.SpannableStringBuilder는 약간 복잡한 방식으로 작동합니다. append 메소드 중 하나를 사용하여 SpannableStringBuilder에 텍스트를 추가 할 수 있습니다. SpannableStringBuilder가 보유하는 텍스트의 일부는 스팬으로 표시 될 수 있으며 해당 부분 만 스타일이 지정됩니다 (색상, 글꼴 등). ), 새 텍스트가 임의의 인덱스에서 SpannableStringBuilder 객체 내에 추가 될 때 새 텍스트도 Spanned 인터페이스의 필드에 의해 정의 된 것으로 표시되는지 여부를 표시합니다. Spannable 인터페이스에 의해 선언되고 SpannableStringBuilder 클래스에 의해 구현 된 setSpan (Object what, int start, int end, int flags) 메서드를 호출합니다. 여기서 android.text.style.StyleSpan 패키지 클래스의 객체는 무엇이며,이 객체는 적용하려는 스타일의 유형을 정의하고, 플래그는 Spanned 인터페이스에 정의 된 필드 중 하나입니다.
4.The different classes of the android.text.style.StyleSpan package defines different styling,but as of now I only know one usage of the android.text.style.StyleSpan class and one usage of the android.text.style.ForegroundColorSpan class. One public constructor of the StyleSpan class takes a int value which may be the value of one of the fields defined in the android.graphics.Typeface class ( BOLD,BOLD_ITALIC,ITALIC,NORMAL ).As these field name suggest they will style the text as bold or bold_italic or italic or normal. For example-
StyleSpan style_span=new StyleSpan(Typeface.ITALIC);
One public constructor of the ForegroundColorSpan class takes a int value which may be any hexadecimal argb color value or may be the value of one of the fields defined in the android.graphics.Color class ( BLACK,BLUE,CYAN,DKGRAY,GRAY,GREEN,LTGRAY,MAGENTA,RED,TRANSPARENT,WHITE,YELLOW ).As the names of these fields suggest they color a text.To give a argb hexadecimal color value as a int to constructor we start the hexadecimal value with a 0X or 0x,java then automatically converts the hexadecimal value into its int equivalent. For example-
ForegroundColorSpan fore_ground_color_Span = new ForegroundColorSpan(Color.RED);
5.In the setSpan(Object what, int start, int end, int flags) method, the start and the end defines the initial range of the text to be marked as spanned,start specifies the index of the starting characters and end specifies the index of the character one passed the ending character,for example- for a String "01234"
new SpannableStringBuilder("01234").setSpan(foreground_color_span, 1, 3, span_flag);
will cause only the 12 be marked as spanned initially,so starting character is the '1' as specified by the start index and the ending character is the 2 as specified by the index (end-1). So if we give the same value for both start and end,then there will be no text covered initially and the range will be zero,for the above example if start and end both are 1,then starting character is 1 but the ending character is 0.this makes no sense in this case there will be no text marked as spanned initially.
6.Now some fields of the Spanned interface work as described below. When a new text is appended.......... the SPAN_EXCLUSIVE_EXCLUSIVE does not mark any text appended before the starting character and after the ending character as spanned,but marks any text appended after the starting character or before the ending character as spanned, SPAN_EXCLUSIVE_EXCLUSIVE is the synonym for SPAN_POINT_MARK,if the range is zero that means no text is marked as spanned initially,then any text appended at any index is not marked as spanned with this flag,
the SPAN_EXCLUSIVE_INCLUSIVE does not mark any text appended before the starting character as spanned,but marks any text appended after the starting character or any text appended before or after the ending character as spanned,according to my tests SPAN_EXCLUSIVE_INCLUSIVE is the synonym for SPAN_POINT_POINT,if the range is zero that means no text is marked as spanned initially,then any text appended at any index is not marked as spanned with this flag,
the SPAN_INCLUSIVE_EXCLUSIVE does not mark any text appended before the ending character as spanned,but marks any text appended after or before the starting character or any text appedned after the ending character as spanned,according to my tests SPAN_INCLUSIVE_EXCLUSIVE is the synonym for SPAN_MARK_MARK,if the range is zero that means no text is marked as spanned initially,then any text appended at any index is not marked as spanned with this flag,
the SPAN_INCLUSIVE_INCLUSIVE marks any text appended after or before the starting and the ending character as spanned,SPAN_INCLUSIVE_INCLUSIVE is the synonym for SPAN_MARK_POINT,if the range is zero that means no text is marked as spanned initially,then any text appended at any index after or before the starting or ending character is always marked as spanned with this flag,
I have not been able understand the other flag constants defined in the Spanned interface properly.
7.Now we can append any text within our SpannableStringBuilder object with one of the append methods or one of the insert methods for example-
SpannableStringBuilder text = new SpannableStringBuilder(); ...... text.append(string_value); //append(CharSequence text) appends given String to the end //insert(int where, CharSequence tb) inserts the given String after the character whose index is (where-1)
The new text inserted to this SpannableStringBuilder will be marked as "spanned and styled" or not ,will depend on the flag we have specified to the previous call to the setSpan(Object what, int start, int end, int flags) method.
8.If we want to apply more than one styling to the text in a SpannableStringBuilder object,then we can call the setSpan(Object what, int start, int end, int flags) method again and specify the styling to the what parameter,and the start and end to be styled initially,then the flag parameter to specify how the styling should behave when any new text is inserted. Here a important thing is that when we call the span method,the styling we specify is paired with the start,end and flag which is independent of any previous call to the span method,that means each call to the span method applies a styling which works independently of any other styling which is also applied to the same text.Even if we apply the same styling again but with a new object to the "what" parameter,this styling also works independently of the previously applied same styling,only way to modify a already applied styling is to specify the same object to the "what" parameter. For example-
TextView txv=(TextView)findViewById(R.id.textView9);
String myString = "01234";
Object ob;
int start = 1;
int end = 4;
int spanFlag = Spannable.SPAN_INCLUSIVE_INCLUSIVE;
SpannableStringBuilder spannableString = new SpannableStringBuilder(myString);
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED);
ob=foregroundSpan;
spannableString.setSpan(foregroundSpan, start, end, spanFlag);
txv.setText(spannableString);
This will result in txv has "01234",where the part "123" is red.
end=3;
spanFlag = Spannable.SPAN_EXCLUSIVE_INCLUSIVE;
spannableString.setSpan(new StyleSpan(Typeface.BOLD), start, end, spanFlag);
txv.setText(spannableString);
This will result in txv has "01234",where the part "123" is red,and the sub-part "12" is both bold (fatty) and red but "3" is only red not bold because of the "end" value.
spannableString.insert(end, "x");
spannableString.insert(start, "x");
txv.setText(spannableString);
This will cause txv to have "0x12x34",where the part "x12x3" being red and only "12x3" being bold, this happened because the red styling was initially applied to the text "123" with the flag SPAN_INCLUSIVE_INCLUSIVE,that means any text appended after or before 1 and 3 are always included in the red styling region,hence "x12x3" being red the bold styling was initially applied to the text "12" with the flag SPAN_EXCLUSIVE_INCLUSIVE,that means any text appended before 1 is not included in the styling region but any text appended after 1 and after or before 2 is always included in the bold styling regio,hence only "12x3" being bold.
int end=3;
spanFlag = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE;
foregroundSpan = new ForegroundColorSpan(Color.RED);
spannableString.setSpan(foregroundSpan, start, end, spanFlag);
txv.setText(spannableString);
This will cause another independent red styling on the text "x1",but since already a other red styling is applied to the text "x12x3",we will see no difference in txv. To actually change the previously applied red styling,we need to pass the same ForegroundColorSpan object as we did when we called the setSpan method on the first occasion,we should use the following code instead of the above,
int end=3;
spanFlag = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE;
spannableString.setSpan(ob, start, end, spanFlag); //ob refers to the same the styling object as we specified on the first occasion
txv.setText(spannableString);
This will cause only "x1" to be red and also if we append any text after "1" or before "x",then that text will not be included in the red styling region,but any text after "x" and before "1" is always included in the red styling region,because of the flag SPAN_EXCLUSIVE_EXCLUSIVE that we specified this time.
9. 마지막으로 스타일링과 스팬 플래그 규칙을 완전히 제거하려면 SpannableStringBuilder 클래스에 의해 구현 된 Spannable 인터페이스에서 정의한 removeSpan (Object what) 메서드를 호출 할 수 있습니다. 동일한 스타일링 개체를 removeSpan 메서드에 전달해야합니다. 우리가 setSpan 메소드에 준 것처럼.
위의 답변 덕분에 특히 색상 예제는 이러한 플래그를 올바르게 이해하는 데 많은 도움이되었습니다.
'Program Tip' 카테고리의 다른 글
오류없이 Symfony2 잘못된 양식 (0) | 2020.12.02 |
---|---|
ostream을 표준 문자열로 변환 (0) | 2020.12.02 |
UITableView 배경 이미지 (0) | 2020.12.02 |
post increment 연산자가 int를 반환하는 메서드에서 작동하지 않는 이유는 무엇입니까? (0) | 2020.12.02 |
if 문에서 여러 옵션 풀기 (0) | 2020.12.02 |