You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

移除外层LinearLayout后自定义TextView属性不生效如何修复?

Fix TextView Attributes Not Taking Effect After Refactoring Custom View to Inherit from TextView

Let's break down why your textColor and other layout attributes aren't working, and how to fix it:

The Root Cause

Your mistake is in the init block of your refactored CustomTimerView:

TextView.inflate(context, R.layout.custom_timer_layout, null)

This line creates a brand new TextView instance from your layout file, but this new TextView has no connection to your CustomTimerView (which is itself a TextView). The attributes defined in custom_timer_layout only apply to this unused TextView, not your actual custom view. Your CustomTimerView retains its default TextView properties (or any properties set where you reference it in XML), hence why text shows up but styling doesn't.

The Fix

You don't need to inflate a separate TextView layout when your custom view is a TextView. Instead, you can either:

Option 1: Set Default Attributes Directly in Code

Keep your custom view lean by setting default styling in the init block, while still allowing XML attributes to override these defaults:

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.widget.TextView
import androidx.core.content.ContextCompat
import java.util.Calendar

class CustomTimerView(context: Context, attrs: AttributeSet) : TextView(context, attrs) {

    init {
        val typedArray = context.obtainStyledAttributes(attrs, android.R.styleable.TextView)
        
        // Set default text color only if not specified in XML
        if (!typedArray.hasValue(android.R.styleable.TextView_textColor)) {
            setTextColor(ContextCompat.getColor(context, R.color.white))
        }
        
        // Set default max lines only if not specified in XML
        if (!typedArray.hasValue(android.R.styleable.TextView_maxLines)) {
            maxLines = 2
        }
        
        // Set default ellipsize only if not specified in XML
        if (!typedArray.hasValue(android.R.styleable.TextView_ellipsize)) {
            ellipsize = TextUtils.TruncateAt.END
        }
        
        typedArray.recycle()
    }

    fun setDates(ob: Promo) {
        val startDate = getStartDate(ob)
        val endDate = getEndDate(ob)
        
        text = when {
            startDate.get(Calendar.MONTH) != endDate.get(Calendar.MONTH) -> 
                getInDifferentMonths(context, ob)
            startDate.get(Calendar.DAY_OF_MONTH) == endDate.get(Calendar.DAY_OF_MONTH) -> 
                getOnlyOneDay(context, ob)
            else -> 
                getInOneMonth(context, ob)
        }
    }

    // Assume these helper methods are implemented elsewhere
    private fun getStartDate(ob: Promo): Calendar = /* Your implementation */
    private fun getEndDate(ob: Promo): Calendar = /* Your implementation */
    private fun getInDifferentMonths(context: Context, ob: Promo): String = /* Your implementation */
    private fun getOnlyOneDay(context: Context, ob: Promo): String = /* Your implementation */
    private fun getInOneMonth(context: Context, ob: Promo): String = /* Your implementation */
}

Option 2: Set Attributes Directly in XML Where You Reference the View

If you prefer to define styling in XML (and override defaults), just set the attributes on your CustomTimerView wherever you use it in your layouts:

<com.yourpackage.CustomTimerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="@color/white"
    android:maxLines="2"
    android:ellipsize="end" />

Since your custom view inherits from TextView, all standard TextView attributes will work out of the box when you set them here.

Key Takeaway

When your custom view is a subclass of an existing View (like TextView), you don't need to inflate a separate layout file for that same View type. Instead, leverage the parent class's built-in support for attributes, and use code to set sensible defaults only when needed.

内容的提问来源于stack exchange,提问作者Strangelove

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:06:29