谷歌搜索我看到很多聲明嵌套片段不能使用XML。現在我是Android的新手,但我的應用程序使用嵌套片段的XML。我還沒有聽衆和接口工作(也許這就是爲什麼人們說你不能使用XML),但GUI的工作原理。爲什麼無法嵌套的片段使用XML?
我的問題:什麼是我讀過關於不使用XML嵌套的碎片的意見是什麼意思?
這裏a link一個說明XML不能與嵌套片段一起使用:
下面的代碼創建3個radiogroups(每個片段)水平地佈置在頂部(在另一個片段)與一個ListView(在另一個片段)在他們下面。這些片段可以很好地控制不同顯示類型的外觀。
這裏是我的代碼:
public class SetupNew extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ngs);
}}
ngs.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<fragment android:id="@+id/frag_options_all"
android:layout_height="250dp"
android:layout_width="fill_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:name="com.EXAMPLE.frag_class_options_all"/>
<fragment android:id="@+id/frag_select_opponent"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:name="com.EXAMPLE.frag_class_opponents"/>
</LinearLayout>
frag_class_options_all.java
public class frag_class_options_all extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.options_all, container, false);
return view;
}}
frag_class_opponents.java個
public class frag_class_opponents extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
//working contact listview
}
options_all.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment android:id="@+id/frag_options"
android:layout_height="fill_parent"
android:layout_width="80dp"
android:layout_marginRight="15dp"
android:name="com.EXAMPLE.frag_class_options"/>
<fragment android:id="@+id/frag_ship_limit"
android:layout_height="fill_parent"
android:layout_width="75dp"
android:layout_marginRight="15dp"
android:name="com.EXAMPLE.frag_class_limit_options"/>
<fragment android:id="@+id/frag_allowable_ship"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:name="com.EXAMPLE.frag_class_allow"/>
</LinearLayout>
frag_class_options,frag_class_limit_options和frag_class_allow都遵循這樣的事情:
public class frag_class_options extends Fragment{
RadioGroup radioGroup;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.options_m, container, false);
radioGroup = (RadioGroup) view.findViewById(R.id.rg_limit);
return view;
}
}
謝謝!我刪除了XML標籤並添加了一個鏈接。 – user2383867
也許混淆是我對「嵌套」的定義。對我來說,嵌套是當一個片段中有另一個片段時,這就是我所擁有的。 – user2383867