问题描述
对于一些原因,我不能使用XML布局文件。
For some reasons I can't use xml layout files.
不过,我需要创建一个平板电脑的Android应用程序。
But I need to create a tablet android app.
我决定采用碎片。
我想创建相同的布局,这个XML生成:
I want to create the same layout that this xml generates:
<?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>
不过,我有问题的片段添加到我的LinearLayout:
But I have problems with adding fragments to my 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;
}
事实上,我甚至不能创建的LinearLayout有两个相同的片段:
Actually I can't even create LinearLayout with two identical fragments:
...
fragmentTransaction.add(0x101, new ItemsList());
fragmentTransaction.add(0x101, new ItemsList());
...
请帮忙
顺便说一句我想不出为什么我们需要声明的FrameLayout为itemDetails片段,但片段是够itemsList ListFragment?
Btw I can't figure out why do we need to declare "FrameLayout" for itemDetails Fragment but "fragment" is enough for itemsList ListFragment?
UPD:
要做到这一点应该只需添加第三个参数:
To do so one should just add third param:
...
fragmentTransaction.add(0x101, new ItemsList(), "uniqueTag1");
fragmentTransaction.add(0x101, new ItemsList(), "uniqueTag2");
...
对于变量参数的默认值是空的,所以我试图创建具有相同的ID两个不同的元素。由于P-LO为他的评论。
Default value for the tag parameter is null, so I was trying to create two different elements with identical ids. Thanks to p-lo for his comment.
推荐答案
我已经找到了解决方案。
I've found the solution.
(更好的方式加入到这个问题。谢谢,P-LO)
(the better way was added to the question. Thanks, p-lo)
如果您希望将多个片段上的活动,你应该创建布局每个片段:
If you want to place more than one fragment on activity you should create layouts for each fragment:
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));
LinearLayout innerLayout1 = new LinearLayout(this);
innerLayout1.setLayoutParams(new LinearLayout.LayoutParams(300, ViewGroup.LayoutParams.FILL_PARENT));
innerLayout1.setId(ITEMS_LIST_ID);
{
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(ITEMS_LIST_ID, new ItemsList());
fragmentTransaction.commit();
}
layout.addView(innerLayout1);
LinearLayout innerLayout2 = new LinearLayout(this);
innerLayout2.setLayoutParams(new LinearLayout.LayoutParams(300, ViewGroup.LayoutParams.FILL_PARENT));
innerLayout2.setId(ITEM_DETAILS_ID);
{
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(ITEM_DETAILS_ID, new ItemDetails());
fragmentTransaction.commit();
}
layout.addView(innerLayout2);
return layout;
}
这篇关于安卓:使用片断编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!