您可以使用Fragment的包與其通信使用哪種佈局。 片段中的類,創建一個靜態方法來創建片段實例並設置它的捆綁,例如:
public static MyFragment newInstance(int layoutId) {
MyFragment f = new MyFragment();
Bundle args = new Bundle();
args.putInt("layoutId", layoutId); // e.g. layoutId = R.layout.layout_fragment_01
f.setArguments(args);
return f;
}
,然後在片段的onCreateView,獲得從包佈局ID:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(getArguments().getInt("layoutId", 0), container, false);
}
那麼,在你的活動,你會做類似如下:
MyFragment fragment = MyFragment.newInstance(R.layout.fragment_layout_01);
getFragmentManager().beginTransaction().add(android.R.id.content, fragment).commit();
請問你爲什麼有這麼多不同的佈局?用戶可以查看這些所有不同的屏幕嗎?這些佈局之間是否有相似之處? –
@ Code-Apprentice 20並不是那麼多。如果你在任何非平凡的應用程序,你至少會有這麼多 –
@GabeSechan我猜我所有的應用程序到目前爲止都是非常微不足道的;-(仍然,我認爲我的後續問題仍然有效 –