0

私は次のようなレイアウトを持っています。いくつかの TextView と ImageView が含まれています。それらの下に、ListFragment を追加したいと思います。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Name: "
        android:id="@+id/textView"
        android:layout_gravity="left|center_vertical" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/name"
        android:layout_toRightOf="@+id/textView"
        android:layout_gravity="left"
        android:text="Large Text" />
...
    <fragment android:id="@+id/list"
        android:layout_below="@id/name"
        class="com.snot.bodyweightworkout.ExerciseListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

現在、以下のコードを使用して ListFragment を追加しています。しかし、上記のように追加したいと思います。同じ方法で追加すると思いますが、レイアウトでどの要素をコンテナとして使用すればよいですか?

FragmentManager fm = getSupportFragmentManager();
if (fm.findFragmentById(android.R.id.content) == null) {
    ProgramListFragment list = new ProgramListFragment();
    fm.beginTransaction().add(android.R.id.content, list).commit();
}

インラインで追加する方法について何か提案はありますか?

4

1 に答える 1

1

ドキュメントを見てください http://developer.android.com/guide/components/fragments.html

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Name: "
    android:id="@+id/textView"
    android:layout_gravity="left|center_vertical" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/name"
    android:layout_toRightOf="@+id/textView"
    android:layout_gravity="left"
    android:text="Large Text" />

<fragment android:name="com.example.news.ArticleListFragment"
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent" />

</RelativeLayout>

フラグメントを取得したいときは、

FragmentManager fm = getSupportFragmentManager();
ProgramListFragment plf = (ProgramListFragment)fm.findFragmentById(r.id.myfragment);

FrameLayoutxml でフラグメントに置き換えて、フレーム内のビューを置き換えることもできます

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Name: "
    android:id="@+id/textView"
    android:layout_gravity="left|center_vertical" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/name"
    android:layout_toRightOf="@+id/textView"
    android:layout_gravity="left"
    android:text="Large Text" />

<FrameLayout
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent" />

</RelativeLayout>

次に、基本的に、追加する代わりに、すでに行っていることを行うことができますreplace

FragmentManager fm = getSupportFragmentManager();
ProgramListFragment list = new ProgramListFragment();
fm.beginTransaction().replace(R.id.list, list).commit();
于 2013-09-12T20:48:12.410 に答える