簡體   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