2011-10-10 98 views
0

我有应该在另一个片段集碎片中的Android活动

在片段的XML的活动中显示一个片段中的位置我试着使用:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/second_fragment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="10sp" 
    android:layout_below="@id/first_fragment"> 
    <TextView 
     android:id="@+id/mytext" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=" " 
     android:focusable="false" 
     android:clickable="false" 
     android:textSize="20sp" 
     android:padding="5sp" 
     android:layout_centerHorizontal="true"/>  
</RelativeLayout> 

(请注意机器人:layout_below = “@ ID/first_fragment” 布局标签)

我也试过这样:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/first_fragment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="10sp">  
    <TextView 
     android:id="@+id/mytext" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=" " 
     android:focusable="false" 
     android:clickable="false" 
     android:textSize="20sp" 
     android:padding="5sp" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@id/first_fragment"/>  
</RelativeLayout> 

(请注意TextView标签中的android:layout_below =“@ id/first_fragment”)

在这两种情况下,应用程序都会编译并运行,但第二个片段显示在屏幕的顶部而不是第一个。

请考虑我用FragmentTransaction编程添加片段,我添加第二个片段,具有添加的第一个之后,但在相同的交易

能否请你告诉我,什么是错的?

感谢信

回答

3

可以使用的FrameLayout(ViewGroup中)在活动XML来保存你的片段,然后加入您的片段对象使用addreplace

fragmentTransaction.replace(R.id.frameLayoutId, yourFragmentObject); 

所以你可以随心所欲对齐

0

这可能是帮助,

<fragment class="com.example.android.hcgallery.TitlesFragment" 
     android:id="@+id/frag_title" 
     android:visibility="gone" 
     android:layout_marginTop="?android:attr/actionBarSize" 
     android:layout_width="@dimen/titles_size" 
     android:layout_height="match_parent" /> 

<fragment class="com.example.android.hcgallery.ContentFragment" 
     android:id="@+id/frag_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

+0

嗯,我没有标签活动的XML。我有两个xml和两个片段类(每个片段一个)。我通过使用FragmentTransaction来添加这两个片段。在两个片段类的onCreateView中,我膨胀通讯员xml来创建并返回片段的视图 – kingston

+0

我必须使用android SDK 3.0或更高版本。 – Thiru

+0

我的意思是我的活动视图是通过使用不包含任何片段的xml来夸大的。我稍后添加片段。我使用兼容性支持库 – kingston

2

我修正了这个问题。在布局我用:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <FrameLayout 
     android:id="@+id/top" 
     android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
    </FrameLayout> 
    <FrameLayout 
     android:id="@+id/bottom" 
     android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_below="@id/top"> 
    </FrameLayout>  
</RelativeLayout> 

那么活动:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
fr1= new Fragment1(); 
ft.add(R.id.top, fr1 , "top"); 
fr2 = new Fragment2(); 
ft.add(R.id.bottom, fr2, "bottom");  
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
ft.commit(); 
getSupportFragmentManager().executePendingTransactions();