我有一个自定义的textView,放置在LinearLayout内
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<TextView
android:id="@+id/timerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="2"
android:ellipsize="end"
tools:text="Only on 22 of July, 2019"
android:textColor="@color/white" />
</LinearLayout>
还有一些逻辑的CustomTimerView
class CustomTimerView(context: Context, attributeSet: AttributeSet) : LinearLayout(context, attributeSet) {
private var textView: TextView
init {
LinearLayout.inflate(context, R.layout.customTimer_layout, this)
textView = findViewById(R.id.timerView)
}
fun setDates(ob: Promo) {
val startDate = getStartDate(ob)
val endDate = getEndDate(ob)
if (startDate.get(Calendar.MONTH) != endDate.get(Calendar.MONTH)) {
textView.text = getInDifferentMonths(context!!, ob)
} else {
if (startDate.get(Calendar.DAY_OF_MONTH) == endDate.get(Calendar.DAY_OF_MONTH)) {
textView.text = getOnlyOneDay(context!!, ob)
} else {
textView.text = getInOneMonth(context!!, ob)
}
}
}
}
我想删除LinearLayout,因为它没有用。但是当我只去喜欢textView时
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/timerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="2"
android:ellipsize="end"
tools:text="Only on 22 of July, 2019"
android:textColor="@color/white" />
和类似的代码类:
class CustomTimerView(context: Context, attributeSet: AttributeSet) : TextView(context, attributeSet) {
init {
TextView.inflate(context, R.layout.custom_timer_layout, null)
}
fun setDates(ob: Promo) {
val startDate = getStartDate(ob)
val endDate = getEndDate(ob)
if (startDate.get(Calendar.MONTH) != endDate.get(Calendar.MONTH)) {
this.text = getInDifferentMonths(context!!, ob)
} else {
if (startDate.get(Calendar.DAY_OF_MONTH) == endDate.get(Calendar.DAY_OF_MONTH)) {
this.text = getOnlyOneDay(context!!, ob)
} else {
this.text = getInOneMonth(context!!, ob)
}
}
}
}
我可以看到一个文本,但是它看起来像是颜色,TextView中的其他参数不起作用。该如何解决?