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つの質問があります:
- 2つのフラグメントを表示するにはどうすればよいですか?
- 最初のフラグメントと2番目のフラグメントの幅を(プログラムで)設定して残りの幅を埋めるにはどうすればよいですか?
ご協力いただきありがとうございます!
PS:
GalleriesFragment
レイアウトがなく、オーバーライドされていませんが、行ごとに次のようなビューが返されますonCreateView
。ArrayAdapter
<?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:
クラスにを追加しましたonCreateView
:GalleriesFragment
@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番目のフラグメントはまだ表示されません...