如何将文本设置为片段内的TextView?

时间:2018-02-02 16:30:48

标签: android

我在片段类的onCreateView方法中尝试了这个片段,但它没有用。

TextView  tv = (TextView)getView().findViewById(R.id.textView_question); 
tv.setText(question);

3 个答案:

答案 0 :(得分:1)

这是您可以实现的方式

View rootView = inflater.inflate(R.layout.fragment_single_image, parent, false);
TextView  tv = (TextView)rootView.findViewById(R.id.textView_question); 
tv.setText(question);

答案 1 :(得分:0)

试试这个

public class YourFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.your_fragment_layout, container, false);

            TextView tv = (TextView) view .findViewById(R.id.textView_question);
            tv .setText(question);

            return view;
        }

    }

答案 2 :(得分:0)

您必须将查看附加到 Textview

 public class DetailFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup    container, Bundle savedInstanceState) {
          View view =  inflater.inflate(R.layout.toolbar_fragment, 
                                     container, false);
            TextView  tv = (TextView)rootView.findViewById(R.id.textView_question);
            return view;
        }
     }

如何使用Fragment doc