Текст не устанавливается в TextView завышенного представления - PullRequest
0 голосов
/ 28 мая 2019

У меня есть пользовательское представление, которое надувается над действием:

class NeedMoreTimeScreen @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr) {

    fun inflate(context: Context) {

        LayoutInflater
            .from(context)
            .inflate(R.layout.screen_more_time, this, true)

        val inflatedView = LayoutInflater
            .from(context)
            .inflate(R.layout.screen_more_time, this, true)

        inflatedView.visibility = View.VISIBLE

        val countDownTextView = inflatedView.findViewById<TextView>(R.id.countDownText)

        countDownTextView.text = "text I want to appear"
    }
}

.xml для пользовательского представления:

<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">

    <RelativeLayout
            android:id="@+id/holder"
            android:layout_width="768px"
            android:layout_height="840px"
            android:background="@color/main_background">

        <TextView
                android:text="10s"
                android:textAppearance="@style/ScreenTitle"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="160px"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/countDownText"/>

    </RelativeLayout>
</FrameLayout>

Текст появляется в TextView, когда я даю ему текст в .xml, но его программная настройка не выполняется. Почему?

1 Ответ

0 голосов
/ 28 мая 2019

Попробуйте код ниже,

public class NeedMoreTimeScreen : RelativeLayout
{
    constructor(context: Context?) : super(context)
    {
        init();
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    {
        init();
    }
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    {
        init();
    }

    private fun init()
    {
        // apply your inflation code here

        val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        layoutInflater.inflate(R.layout.screen_more_time, this, true)

        countDownTextView.text = "text I want to appear"
    }
}
...