我在这里遇到了一个奇怪的问题。我只是想为 fragment 中的 TextView 设置一个值。问题是它根本没有得到更新。颜色也没有变化。虽然显示了预定义文本“test”,因此我可以排除任何与布局相关的问题。 对此有什么想法吗? 谢谢!!
fragment 类.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/classdetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test"
android:textColor="#000" />
</RelativeLayout>
fragment .java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//datasource = new ClassesDataSource(getActivity());
//datasource.open();
//Classes value = datasource.getClasses(this.getArguments().getString("id"));
View v = inflater.inflate(R.layout.fragment_class, container, false);
TextView tv = (TextView) v.findViewById(R.id.classdetail);
tv.setText("test_IDE");
tv.setTextColor(Color.BLUE);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_class, container, false);
}
最佳答案
你的问题是你将 View 膨胀了两次。
替换:
return inflater.inflate(R.layout.fragment_class, container, false);
与:
return v;
为了解释问题所在,您正在膨胀 View ,设置文本和文本颜色,然后膨胀一个不同的 View 并将该 View 指定为将用于 fragment 的 View .
关于java - TextView 没有得到更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19596953/