0

私はレイアウトファイルを次のように持っています:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:fillViewport="true"
    android:gravity="top" >

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

        <LinearLayout
            android:id="@+id/foodItemActvity_linearLayout_fragments_row1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" 
            android:weightSum="2">
        </LinearLayout>
    </LinearLayout>
</ScrollView>

私のコードでは、fragment_row1 という名前の LinearLayout に 2 つのフラグメントをプログラムで追加します。

しかし、私のフラグメントのレイアウト ファイルでは、線形レイアウトの重みを設定できませんか? 両方のフラグメントを 50% のウェイト レイアウトで並べて表示したいと考えています。

2 つのフラグメント レイアウトのコードは次のとおりです。

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:gravity="center"
    android:text="IMAGE" />

<ImageView
    android:id="@+id/fragment_image_imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="Product Image"
    android:src="@drawable/call_waiter_icon" />

と:

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text Box" />

これと同等にしたいのですが、プログラムでフラグメントを追加する必要があります。

 <LinearLayout
            android:id="@+id/foodItemActvity_linearLayout_fragments_row1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="2" >

            <fragment
                android:id="@+id/fragment1"
                android:name="dds.ImageFragment"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"

                tools:layout="@layout/fragment_dds_image" />

            <fragment
                android:id="@+id/fragment2"
                android:name="dds.RatingFragment"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                tools:layout="@layout/fragment_dds_rating" />
        </LinearLayout>

編集:

      <LinearLayout
            android:id="@+id/foodItemActvity_linearLayout_fragments_row1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="2" >

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

            <FrameLayout
                android:id="@+id/frame2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" >
            </FrameLayout>
        </LinearLayout>

フラグメントを追加するコード

getSupportFragmentManager().beginTransaction().add(R.id.frame1, frag1, "fragment_grandchild1" + fragCount).commit();        
getSupportFragmentManager().beginTransaction().add(R.id.frame2, frag2, "fragment_grandchild2" + fragCount).commit();
4

0 に答える 0