Android:以编程方式使用片段

时间:2011-07-29 10:19:19

标签: android android-layout android-fragments

由于某些原因,我无法使用xml布局文件。

但我需要创建一个平板电脑安卓应用程序。

我决定使用片段。

我想创建此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>

但我在向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:

            ...
            fragmentTransaction.add(0x101, new ItemsList());
            fragmentTransaction.add(0x101, new ItemsList());
            ...

请帮忙

顺便说一下,我无法弄清楚为什么我们需要为itemDetails片段声明“FrameLayout”,但“fragment”对于itemsList ListFragment来说已经足够了?

UPD:

为此,我们应该添加第三个参数:

            ...
            fragmentTransaction.add(0x101, new ItemsList(), "uniqueTag1");
            fragmentTransaction.add(0x101, new ItemsList(), "uniqueTag2");
            ...

tag参数的默认值为null,因此我尝试使用相同的id创建两个不同的元素。感谢p-lo的评论。

2 个答案:

答案 0 :(得分:6)

我找到了解决方案。

(问题中添加了更好的方法。谢谢,p-lo)

如果要在活动上放置多个片段,则应为每个片段创建布局:

    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;


    }

答案 1 :(得分:2)

您缺少的是LayoutInflator。请参阅活动中的此示例:

private void createPolicyTable() {

    if (customerInfo.getPolicies() == null || customerInfo.getPolicies().isEmpty()) {
        Log.v(TAG, "no policies were found.");
        return;
    }

    LinearLayout policyTable = (LinearLayout)findViewById(R.id.manage_my_policy_policy_table);      
    LayoutInflater inflater = getLayoutInflater();
    for(int i = 0; i < customerInfo.getPolicies().size(); i++) {
        Policy policy = customerInfo.getPolicies().get(i);

        LinearLayout row = (LinearLayout)inflater.inflate(R.layout.policy_table_row, null);

        ImageView type = (ImageView)row.findViewById(R.id.policy_table_row_type);

        if (policy instanceof PersonalVehiclePolicy) {
            type.setImageResource(R.drawable.auto_policy);
        }
        else if (policy instanceof HomeownersPolicy) {
            type.setImageResource(R.drawable.home_policy);
        }

        ((TextView)row.findViewById(R.id.policy_table_row_policy_number)).setText(policy.getPolicyNumber());
        ((TextView)row.findViewById(R.id.policy_table_row_amount_due)).setText(policy.getAmountDue());
        ((TextView)row.findViewById(R.id.policy_table_row_due_date)).setText(policy.getDueDate());

        row.setOnClickListener(createPolicyDetailsOnClickListener(policy));

        policyTable.addView(row);

    }

}

以及正在膨胀的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android" style="@style/ListItem">

    <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
      android:stretchColumns="*">
        <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" 
          android:paddingBottom="5dp">
            <TextView android:text="@string/type_label" style="@style/RowLabel" />

            <ImageView android:id="@+id/policy_table_row_type" android:layout_width="wrap_content"
              android:layout_height="wrap_content" />         
            <TextView style="@style/RowLabel" android:text="@string/policy_num_label" 
              android:paddingRight="5dp" />
            <TextView android:id="@+id/policy_table_row_policy_number" style="@style/DefaultText" />
        </TableRow>

        <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" 
          android:paddingBottom="5dp">
            <TextView style="@style/RowLabel" android:text="@string/due_label" android:paddingRight="5dp" />
            <TextView android:id="@+id/policy_table_row_amount_due" style="@style/DefaultText" android:paddingRight="5dp" />
            <TextView style="@style/RowLabel" android:text="@string/on_lc" android:paddingRight="5dp" />
            <TextView android:id="@+id/policy_table_row_due_date" style="@style/DefaultText" />
        </TableRow>
    </TableLayout>


</LinearLayout>