TextView textView = (TextView) rootView.findViewById(R.id.textview_id); textView.setText("Hello World");
如果文本内容来自于其他变量或资源文件,需要确保正确地获取到对应的值,例如:
String text = getString(R.string.hello_world); textView.setText(text);
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_layout, container, false); TextView textView = (TextView) rootView.findViewById(R.id.textview_id); String text = getString(R.string.hello_world); textView.setText(text); return rootView; }
通过以上步骤,可以确保TextView在Fragment中正确设置和显示文本内容。