【发布时间】:2019-04-15 04:46:30
【问题描述】:
我想在不使用任何xml 文件的情况下制作fragment 示例
我尝试仅使用 java 制作 layout。
之后,我想把它附加到fragment。
但是override fragment 中的class 必须使用inflate 返回xml 文件。
(对吗?)
另外,由于fragment 不是Activity,我不能在上面使用setContentView。
我该如何解决这个问题?
public class FragmentA extends Fragment {
public FragmentA() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LinearLayout linear = new LinearLayout(getContext());
linear.setOrientation(LinearLayout.VERTICAL);
linear.setBackgroundColor(Color.WHITE);
TextView tv = new TextView(getContext());
tv.setText("Hello World1");
linear.addView(tv);
return inflater.inflate((XmlPullParser) linear, null);
}
}
我想在不使用任何 xml 文件的情况下制作片段示例
【问题讨论】:
-
更改为
return linear;。就是这样。 -
linear是View实例不需要使用inflate方法,只需从onCreateView返回linear实例 -
我想得太远了...谢谢!
标签: android android-layout android-activity fragment