我是新来的。我有一个用于片段收集对象的XML。在我有两个文本视图,完美地工作。当我尝试以完全相同的格式添加另一个时,接收无法解决错误。(即:android:id =“ @ android:id / tvNewText”)。如果我添加一个类似于常规XML的ID(即android:id =“ @ + id / tvAnotherNewText”),则无法从我的Fragment onCreateView访问它。有什么帮助吗?fragment_collection_object.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="40sp"
android:padding="32dp"
android:text="Sports Bar" />
这可以正常工作,但是在它的正下方,我添加了一个新的TextView,它无法解析ID。
<TextView
android:id="@android:id/tvNewText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Some Words" />
尝试从下面的代码访问:
public static class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_collection_object, container, false);
Bundle args = getArguments();
((TextView) rootView.findViewById(android.R.id.text1)).setText((args.getString(ARG_OBJECT)));
((TextView) rootView.findViewById(android.R.id.tvNewText)).setText("heyo");
return rootView;
}
}
在这里,text1可以正常工作,并且符合预期,但无法在tvNewText上解析。在此先感谢您的帮助。
使用android:id="@+id/tvNewText"
解决错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句