Android 是否有任何方法可以通过编程方式使用kotlin更改片段中的文本颜色?
这是示例代码,我需要在特定条件下更改文本视图中的文本颜色。比如,不同月份的文字颜色应该不同。 多谢各位 碎片1.ktAndroid 是否有任何方法可以通过编程方式使用kotlin更改片段中的文本颜色?,android,xml,android-studio,android-fragments,kotlin,Android,Xml,Android Studio,Android Fragments,Kotlin,这是示例代码,我需要在特定条件下更改文本视图中的文本颜色。比如,不同月份的文字颜色应该不同。 多谢各位 碎片1.kt import android.os.Bundle import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup class FragmentOne : Fragment() {
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class FragmentOne : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one, container, false)
}
}
fragment_one.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".FragmentOne">
<TextView
android:id="@+id/textview"
android:textAlignment="center"
android:layout_gravity="center"
android:background="#7654ad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="me !" />
</FrameLayout>
您可以在onCreateView内部尝试此方法吗
var view = inflater.inflate(R.layout.fragment1, container, false);
var textView : TextView = view?.findViewById(R.id.textview) as TextView
//your condition,then set the color. Use if or when
textView.setTextColor(Color.RED)
你尝试了什么?我正在尝试做一个日历应用程序。每个片段都是几个月,在片段中有些按钮或一些视图持续了几天。现在我的问题是,如何突出显示当前日期。如果可能的话,请帮忙。谢谢你,很抱歉我的英语不好。这取决于你是否使用某种日历视图或其他东西,或者只是一个简单的文本视图。不使用日历视图,因为我正在尝试制作本地语言日历,这意味着每天都没有本地语言数字。所以我用的是简单的cardview或textview。你试过了吗:?我会试的,谢谢!