[ Spannable ]
: TextView를 여러 개 쓰지 않아도 하나의 TextView에서 해당 text를 부분적으로 바꿀 수 있다.
- SpnnableUtils.java
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
/**
* Created by gpark on 2017-01-31.
*/
public class SpannableUtils {
/**
* @param text : 적용할 텍스트
* @param color : ex) Color.RED, Color.rgb(255, 0, 0), getResource().getColor(R.color.red), ...
* @param start : 적용할 텍스트의 시작 index
* @param end : 적용할 텍스트의 끝 index
* @return
*/
public static Spannable changeForegroundTextColor(String text, int color, int start, int end) {
SpannableStringBuilder sp = new SpannableStringBuilder(text);
sp.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sp;
}
/**
*
* @param text : 적용할 텍스트
* @param proportion : 텍스트 사이즈 비율
* @param start : 적용할 텍스트의 시작 index
* @param end : 적용할 텍스트의 끝 index
* @return
*/
public static Spannable changeTextRelativeSize(String text, float proportion, int start, int end) {
SpannableStringBuilder sp = new SpannableStringBuilder(text);
sp.setSpan(new RelativeSizeSpan(proportion), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sp;
}
/**
*
* @param text : 적용할 텍스트
* @param fontSize : 적용할 텍스트의 size
* @param start : 적용할 텍스트의 시작 index
* @param end : 적용할 텍스트의 끝 index
* @return
*/
public static Spannable changeTextAbsoluteSize(String text, int fontSize, int start, int end) {
SpannableStringBuilder sp = new SpannableStringBuilder(text);
sp.setSpan(new AbsoluteSizeSpan(fontSize), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sp;
}
/**
* 두 가지 spannable 동시에 적용 가능
* @param text : 적용할 텍스트
* @param fontSize : 적용할 텍스트의 size
* @param color : ex) Color.RED, Color.rgb(255, 0, 0), getResource().getColor(R.color.red), ...
* @param start : 적용할 텍스트의 시작 index
* @param end : 적용할 텍스트의 끝 index
* @return
*/
public static Spannable changeTextSizeAndColor(String text, int color, int fontSize, int start, int end) {
SpannableStringBuilder sp = new SpannableStringBuilder(text);
sp.setSpan(new AbsoluteSizeSpan(fontSize), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
sp.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sp;
}
/**
* 추가 예정
*/
}
- MainActivity.java
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String text = "안녕하세요. 부족한 글 읽어주셔서 감사합니다.^^";
TextView textView = (TextView) findViewById(R.id.change_text_color);
textView.append(SpannableUtils.changeForegroundTextColor(text, Color.CYAN, 3, 15));
textView = (TextView) findViewById(R.id.change_text_relative_size);
textView.append(SpannableUtils.changeTextRelativeSize(text, 0.8f, 0, 6));
textView = (TextView) findViewById(R.id.change_absolute_text_size);
textView.append(SpannableUtils.changeTextAbsoluteSize(text, 30, 10, text.length()));
textView = (TextView) findViewById(R.id.change_text_size_and_color);
textView.append(SpannableUtils.changeTextSizeAndColor(text, Color.RED, 30, 5, 14));
}
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.phg.practice.MainActivity">
<TextView
android:id="@+id/change_text_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Spannable Test : "
android:textSize="20dp"
android:textColor="#000"/>
<TextView
android:layout_marginTop="10dp"
android:id="@+id/change_text_relative_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Spannable Test : "
android:textSize="20dp"
android:textColor="#000"/>
<TextView
android:layout_marginTop="10dp"
android:id="@+id/change_absolute_text_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Spannable Test : "
android:textSize="20dp"
android:textColor="#000"/>
<TextView
android:layout_marginTop="10dp"
android:id="@+id/change_text_size_and_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Spannable Test : "
android:textSize="20dp"
android:textColor="#000"/>
</LinearLayout>
- 예시 화면
자세한 내용은 추후에 추가할 예정입니다...
'Programming > Android' 카테고리의 다른 글
[Android] 선택 되었을 때 Text Color 바꾸기 (0) | 2017.05.16 |
---|---|
[Android] Square ImageView (정사각형 ImageView) (0) | 2017.05.03 |
[Android] GPS 설정 체크하기 (0) | 2016.12.18 |
[Android] Volley & Patch request (0) | 2016.12.18 |
[Android] Activity Life Cycle(생명주기) (1) | 2015.12.14 |