728x90
build.gradle (:app)
plugins {
id 'com.android.application'
}
android {
compileSdk 30
defaultConfig {
applicationId "com.devshin93.spannabletest"
minSdk 25
targetSdk 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures {
viewBinding true
dataBinding true
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MaginActivity.java
์ฃผ์์ ํ๋ํ๋ ๋ฐ๊ฟ ๊ฐ๋ฉฐ ํ์ธํด ๋ณด์.
package com.devshin93.spannabletest;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import androidx.appcompat.app.AppCompatActivity;
import com.devshin93.spannabletest.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
SpannableString str = new SpannableString("Red");
SpannableStringBuilder resultBuilder = new SpannableStringBuilder(str);
int begin = 0;
int end = resultBuilder.length();
resultBuilder.setSpan(new ForegroundColorSpan(Color.RED), begin, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); // Red Black
// resultBuilder.setSpan(new ForegroundColorSpan(Color.RED), begin, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // Red Red
// resultBuilder.setSpan(new ForegroundColorSpan(Color.RED), begin, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); // Red Red
// resultBuilder.setSpan(new ForegroundColorSpan(Color.RED), begin, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // Red Black
resultBuilder.append("Black");
binding.textView.setText(resultBuilder);
}
}
1๋ฒ, 4๋ฒ ๊ฒฐ๊ณผ
Spannable.SPAN_INCLUSIVE_EXCLUSIVE
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
2๋ฒ, 3๋ฒ ๊ฒฐ๊ณผ
Spannable.SPAN_INCLUSIVE_INCLUSIVE
Spannable.SPAN_EXCLUSIVE_INCLUSIVE
๋๊ธ