3

2つのフラグメントでアクティビティを作成しようとしています

  • 最初のフラグメントは、LisFragmentを使用したギャラリーのリストを示しています
  • 2番目のフラグメントは、最初のフラグメントで選択されたギャラリーのプレビュー画像を表示します

ビューでフラグメントを宣言すると、機能します。しかし今、私はフラグメントを動的に管理したいと思います:

GalleriesFragment gfs = new GalleriesFragment();
GalleryFragment gf = new GalleryFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.layout_m,gfs);
ft.add(R.id.layout_m,gf);
ft.commit();

私のレイアウト:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:id="@+id/layout_m">
</LinearLayout>

これを行うと、最初に追加されたフラグメントのみが表示され、フラグメントを追加するときに順序を逆にしようとしました...

レイアウトで作業しているときに、フラグメント幅を次のように設定できました。

android:layout_width="400dp"

今私は2つの質問があります:

  1. 2つのフラグメントを表示するにはどうすればよいですか?
  2. 最初のフラグメントと2番目のフラグメントの幅を(プログラムで)設定して残りの幅を埋めるにはどうすればよいですか?

ご協力いただきありがとうございます!

PS:

GalleriesFragmentレイアウトがなく、オーバーライドされていませんが、行ごとに次のようなビューが返されますonCreateViewArrayAdapter

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/idimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10sp"
        android:layout_marginLeft="10sp"
        android:layout_marginTop="10sp"
        android:contentDescription="preview"
         >
    </ImageView>

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/texttitre"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5sp"
            android:layout_marginRight="7sp"
            android:layout_marginTop="10sp"
            android:background="@layout/roundcorner"
            android:gravity="center"
            android:text="Title here" >
        </TextView>

        <TextView
            android:id="@+id/itemdescription"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10sp"
            android:layout_marginRight="10sp"
            android:text="description" >
        </TextView>

    </LinearLayout>

</LinearLayout>

GalleryFragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

アップデート2:

クラスにを追加しましたonCreateViewGalleriesFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        FrameLayout.LayoutParams lparams =  new FrameLayout.LayoutParams(400, LayoutParams.MATCH_PARENT);
        container.setLayoutParams(lparams);
        return super.onCreateView(inflater, container, savedInstanceState);
}

これにより、最初のフラグメントの幅が修正されますが、2番目のフラグメントはまだ表示されません...

4

1 に答える 1

2

2 つのフラグメントを表示するにはどうすればよいですか?

現在のアプローチでは、addメソッドに提供される最初のフラグメントを作成して、コンテナー レイアウトのすべてのスペースを占有します。1 つの解決策は、次のように、追加される 2 つのフラグメントのそれぞれにスペースを予約するレイアウトを使用することです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_m"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <FrameLayout
        android:id="@+id/frame1"
        android:layout_width="100dp"
        android:layout_height="match_parent" />

    <FrameLayout
        android:id="@+id/frame2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

次にコードで:

ft.add(R.id.frame1, gfs);
ft.add(R.id.frame2, gf);
ft.commit();

最初のフラグメントと 2 番目のフラグメントの幅を (プログラムで?) 設定して、残りの幅を埋めるにはどうすればよいですか?

フラグメントは単なる通常のビューではありませんが、それを行う 1 つの方法は、単一のLinearLayoutレイアウト ファイルを使用してLayoutParams、フラグメントのビューを に設定することonActivityCreatedです。

getView().setLayoutParams(new LinearLayout.LayoutParams(100, LinearLayout.LayoutParams.WRAP_CONTENT));

もちろん、これによりフラグメントが親コンテナーに関連付けられますが、これがフラグメント システム全体でどの程度うまく機能するかはわかりません。

于 2012-10-07T10:29:28.793 に答える