java如何从函数中添加片段?
我想在屏幕上创建背景片段,用于加载查询
它不工作,我在屏幕上看不到碎片
public class internet {
startQueryAsync(final Activity activity) {
new Thread() {
RelativeLayout backView = new RelativeLayout(activity);
@Override
public void run() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
FragmentLoad fr = new FragmentLoad();
activity.getFragmentManager().beginTransaction().add(安卓.R.id.content, fr);
}
});
}
}.start();
}
}
public class FragmentLoad extends Fragment {
public FragmentLoad() {
super();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_load, container);
view.setLayoutParams(lpFr);
return view;
}
}
我不明白为什么它不起作用
# 1 楼答案
您尚未提交您的碎片事务。像这样改变你的方法
# 2 楼答案
你忘了做
fragment
的commit();
例如:
您也可以使用Fluent Interface Pattern来执行以下操作:
有关更多详细信息,请参阅Google Fragment Documentation
希望这有帮助