我在这里面临一个奇怪的问题。我只是想在Fragment中为TextView设置一个值。问题在于它根本不会更新。颜色也不会改变。尽管显示了预定义的文本“ test”,所以我可以排除任何与布局有关的问题。
有什么想法吗?
谢谢 !!
fragment_class.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);
}
最佳答案
您的问题是您两次放大视图。
更换:
return inflater.inflate(R.layout.fragment_class, container, false);
与:
return v;
为了解释问题所在,您先对视图进行放大,设置文本和文本颜色,然后对另一视图进行膨胀,然后将该视图分配为用于片段的视图。
关于java - TextView没有更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19596953/