在Java代码中添加片段时,片段不会填充布局

时间:2014-05-22 16:12:52

标签: android android-layout android-fragments

我的布局如下图所示:

enter image description here

并且布局中的以下全部按钮是线性布局我将myFragment添加到。

但是当我把MyFragment放到线性布局时。它没有像这样显示填充到linearlayout:

enter image description here

如何将片段填充到布局?

这里更新的是xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="fi"
android:layout_height="match_parent"
android:background="#FFee00"
tools:context=".MainActivity" >

<Button
    android:id="@+id/btnAttachA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/btnReplaceBwithA"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/btnReplaceBwithA"
    android:text="Attach A " />

<Button
    android:id="@+id/btnRemoveB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btnRemoveA"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="21dp"
    android:text="remove b" />

<Button
    android:id="@+id/btnRemoveA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="RemoveA" />

<Button
    android:id="@+id/btnAddB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/btnRemoveA"
    android:text="Add B" />

<Button
    android:id="@+id/btnAddA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/btnAddB"
    android:text="add A" />

<Button
    android:id="@+id/btnReplaceBwithA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/btnRemoveA"
    android:text="replace B with A" />

<Button
    android:id="@+id/btnDettachA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/btnReplaceBwithA"
    android:text="Dettach A" />

<Button
    android:id="@+id/btnReplaceAwithB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/btnDettachA"
    android:layout_alignBottom="@+id/btnDettachA"
    android:layout_toRightOf="@+id/btnDettachA"
    android:text="replace A with B" />

<LinearLayout
    android:id="@+id/layoutforrepalce"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/btnDettachA"
    android:orientation="vertical" >
</LinearLayout>

这里是片段A的xml

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="this is Fragment A"
    android:textAppearance="?android:attr/textAppearanceLarge" />

1 个答案:

答案 0 :(得分:0)

您在xml文件中使用wrap_content的可能性很大。尝试在android :: layout_height中使用match_parent或fill_parent。