반응형
textSize가 다른 TextView
하나의 TextView에서 다른 textSize를 설정할 수 있습니까? 다음을 사용하여 텍스트 스타일을 변경할 수 있다는 것을 알고 있습니다.
TextView textView = (TextView) findViewById(R.id.textView);
Spannable span = new SpannableString(textView.getText());
span.setSpan(arg0, 1, 10, arg3);
textView.setText(span)
크기를 변경하려는 텍스트의 범위 시작 ... 끝을 알고 있습니다. 그러나 나는 무엇을로 사용할 수 있습니다 arg0
와 arg3
?
시험
span.setSpan(new RelativeSizeSpan(0.8f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
대답하기 늦었지만 사람들은 여전히 똑같은 질문을 할 수 있습니다. strings.xml 파일에이 두 문자열이 있다고 가정합니다.
<string name="my_text">You will need a to complete this assembly</string>
<string name="text_sub1">screwdriver, hammer, and measuring tape</string>
이제 다른 textSize를 사용하여 style.xml 내에 두 가지 스타일을 정의해야합니다.
<style name="style0">
<item name="android:textSize">19sp</item>
<item name="android:textColor">@color/standout_text</item>
<item name="android:textStyle">bold</item>
</style>
<style name="style1">
<item name="android:textSize">23sp</item>
<item name="android:textColor">@color/standout_light_text</item>
<item name="android:textStyle">italic</item>
</style>
이제 Java 파일에서 spannable을 사용하여 단일 textView에이 두 스타일과 문자열을로드해야합니다.
SpannableString formattedSpan = formatStyles(getString(R.string.my_text), getString(R.string.text_sub0), R.style.style0, getString(R.string.main_text_sub1), R.style.style1);
textView.setText(formattedSpan, TextView.BufferType.SPANNABLE);
다음은 스타일을 적용한 후 형식이 지정된 문자열을 반환하는 formatStyles 메서드입니다.
private SpannableString formatStyles(String value, String sub0, int style0, String sub1, int style1)
{
String tag0 = "{0}";
int startLocation0 = value.indexOf(tag0);
value = value.replace(tag0, sub0);
String tag1 = "{1}";
int startLocation1 = value.indexOf(tag1);
if (sub1 != null && !sub1.equals(""))
{
value = value.replace(tag1, sub1);
}
SpannableString styledText = new SpannableString(value);
styledText.setSpan(new TextAppearanceSpan(getActivity(), style0), startLocation0, startLocation0 + sub0.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
if (sub1 != null && !sub1.equals(""))
{
styledText.setSpan(new TextAppearanceSpan(getActivity(), style1), startLocation1, startLocation1 + sub1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return styledText;
}
AbsoluteSizeSpan으로 시도
snackbarText.setSpan(new AbsoluteSizeSpan(fontsize, true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
완전한 코드는 다음과 같습니다.
SpannableStringBuilder snackbarText = new SpannableStringBuilder();
snackbarText.append("Your text");
snackbarText.setSpan(new AbsoluteSizeSpan(fontsize, true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Snackbar.make(getCurrentFocus(), snackbarText, Snackbar.LENGTH_LONG).setAction("Action", null).show();`
참고URL : https://stackoverflow.com/questions/7388361/textview-with-different-textsize
반응형
'developer tip' 카테고리의 다른 글
반응 : 차이점 (0) | 2020.10.05 |
---|---|
두 배열의 값이 동일한 지 확인하는 방법 (0) | 2020.10.04 |
Java 기본 요소 배열이 스택 또는 힙에 저장됩니까? (0) | 2020.10.04 |
브라우저 / HTML src =“data : image / jpeg; base64…”에서 이미지 강제 다운로드 (0) | 2020.10.04 |
두 배열의 내용이 동일한 지 확인합니다 (순서에 상관 없음). (0) | 2020.10.04 |