由于某些原因,我不能使用xml布局文件.
但我需要创建一个平板电脑Android应用程序.
我决定使用碎片.
我想创建此xml生成的相同布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<fragment class="com.example.ItemsList"
android:id="@+id/items_list" android:layout_weight="1"
android:layout_width="0dp" android:layout_height="fill_parent" />
<FrameLayout android:id="@+id/item_details" android:layout_weight="1"
android:layout_width="0dp" android:layout_height="fill_parent"
android:background="?android:attr/detailsElementBackground" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
但我在向linearLayout添加片段时遇到问题:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(createUI());
}
private View createUI() {
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(new LinearLayout.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, AbsListView.LayoutParams.FILL_PARENT));
layout.setId(0x101);
{
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(0x101, new ItemsList());
fragmentTransaction.add(0x101, new ItemDetails());
fragmentTransaction.commit();
}
return layout;
}
Run Code Online (Sandbox Code Playgroud)
实际上我甚至无法使用两个相同的片段创建LinearLayout:
...
fragmentTransaction.add(0x101, new ItemsList());
fragmentTransaction.add(0x101, new ItemsList());
...
Run Code Online (Sandbox Code Playgroud)
请帮忙
顺便说一句我无法弄清楚为什么我们需要为itemDetails片段声明"FrameLayout",但"fragment"对于itemsList ListFragment来说已经足够了?
UPD:
为此,我们应该添加第三个参数: …