在android中动态调整片段大小

在android中动态调整片段大小,android,android-layout,android-fragments,android-xml,Android,Android Layout,Android Fragments,Android Xml,我有一个片段填满了整个屏幕。现在,如果我添加一个新的片段,我希望每个片段都占据屏幕的一半。我该怎么做 我尝试了这种布局,并以编程方式添加了片段。但它不起作用 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" an

我有一个片段填满了整个屏幕。现在,如果我添加一个新的片段,我希望每个片段都占据屏幕的一半。我该怎么做

我尝试了这种布局,并以编程方式添加了片段。但它不起作用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="tech.test.org.game.Game$PlaceholderFragment">


    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="50">


    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_below="@+id/relativeLayout"
        android:layout_weight="50"
        >
        </RelativeLayout>

</RelativeLayout>


谢谢。

使用线性布局而不是相对布局,并将布局设置为

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

    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="100" >

        <fragment
            android:id="@+id/fragment1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        </fragment>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="50"
        android:visibility="gone" >

        <fragment
            android:id="@+id/fragment2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        </fragment>
    </LinearLayout>

</LinearLayout>

Kat-hat给出的答案也是正确的,但是要加载UI需要很大的负载,这可能会影响性能

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
   >

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:visibility="gone" >

        </RelativeLayout>

</LinearLayout>


但是如果你这样做,那么片段A的一半是隐藏的,并且没有调整大小,对吗?
    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
   >

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:visibility="gone" >

        </RelativeLayout>

</LinearLayout>
findViewById(R.id.relativeLayout2).setVisibility(View.VISIBLE);