使用 Kotlin 语言在 Android Studio 中设置文本的渐变颜色
Set gradient color for text in Android Studio with Kotlin language
我有一个文本,我想设置一个渐变色作为它的前景。我可以在 XML 完成吗?
或者我应该在 activity 中完成吗?我正在 Android Studio 中使用 Kotlin 编程。
文件activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="center"
android:text="Text Color is Gradient"
android:textColor="#000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
文件gradient.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:angle="90"
android:centerColor="#555994"
android:endColor="#b5b6d2"
android:startColor="#555994"
android:type="linear" />
<corners
android:radius="0dp"/>
</shape>
我的activity是这样的:
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
为了将渐变设置为 Textview
文本颜色,您必须使用 textshader
解析渐变颜色。您可以根据需要自定义颜色。
val paint = textView.paint
val width = paint.measureText(textView.text.toString())
val textShader: Shader = LinearGradient(0f, 0f, width, textView.textSize, intArrayOf(
Color.parseColor("#F97C3C"),
Color.parseColor("#FDB54E"),
/*Color.parseColor("#64B678"),
Color.parseColor("#478AEA"),*/
Color.parseColor("#8446CC")
), null, Shader.TileMode.REPEAT)
textView.paint.setShader(textShader)
我有一个文本,我想设置一个渐变色作为它的前景。我可以在 XML 完成吗?
或者我应该在 activity 中完成吗?我正在 Android Studio 中使用 Kotlin 编程。
文件activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="center"
android:text="Text Color is Gradient"
android:textColor="#000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
文件gradient.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:angle="90"
android:centerColor="#555994"
android:endColor="#b5b6d2"
android:startColor="#555994"
android:type="linear" />
<corners
android:radius="0dp"/>
</shape>
我的activity是这样的:
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
为了将渐变设置为 Textview
文本颜色,您必须使用 textshader
解析渐变颜色。您可以根据需要自定义颜色。
val paint = textView.paint
val width = paint.measureText(textView.text.toString())
val textShader: Shader = LinearGradient(0f, 0f, width, textView.textSize, intArrayOf(
Color.parseColor("#F97C3C"),
Color.parseColor("#FDB54E"),
/*Color.parseColor("#64B678"),
Color.parseColor("#478AEA"),*/
Color.parseColor("#8446CC")
), null, Shader.TileMode.REPEAT)
textView.paint.setShader(textShader)