繁体   English   中英

TextView没有更新

[英]TextView doesn't get updated

我在这里面临一个奇怪的问题。 我只是想在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;

view and assigning that one as the one that will be used for the fragment. 为了解释问题所在,您先对视图进行扩展,设置文本和文本颜色,然后对视图进行扩展,然后将该视图分配为用于片段的视图。

尝试使用

View v =  getActivity().getLayoutInflater().inflate(R.layout.fragment_class, container, false);

代替

View v =  inflater.inflate(R.layout.fragment_class, container, false);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM