膨胀视图后膨胀的视图方法不起作用

问题描述

我有一个函数可以扩展布局,并应该操纵布局中包含的视图。布局的基本元素是 ConstraintLayout

片段:

public class HomeFragment extends Fragment {
    ...
    public void myFunction() {
        View v = getLayoutInflater().inflate(R.layout.fragment_home,null);
        TextView t = (TextView) v.findViewById(R.id.my_text_view);
        t.setText("Some other text"); // nothing happens
        t.setTextColor(0xFF0000FF); // nothing happens
        t.setBackgroundColor(0xFFFF0000); // nothing happens
        Log.d("call","myFunction() was called");
    }
}

布局:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/my_text_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#F0F0F0"
        android:text="TextView"
        android:textColor="#000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

<androidx.constraintlayout.widget.ConstraintLayout/>

代码编译并成功运行,但是当我调用 myFunction() 时,TextView 没有任何反应,而日志语句则成功记录。我做错了什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...