【问题标题】:Set fragments' positions in android activities在android活动中设置片段的位置
【发布时间】:2011-10-10 13:52:55
【问题描述】:

我有一个片段应该显示在另一个片段下的活动中

在我尝试使用的片段的 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>

(请注意布局标签中的android: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 以编程方式添加片段,并且在添加第一个片段之后添加第二个片段,但在同一个事务中

你能告诉我有什么问题吗?

谢谢

【问题讨论】:

    标签: android position fragment


    【解决方案1】:

    您可以在活动 xml 中使用 FrameLayout (viewGroup) 来保存片段,然后使用 addreplace 添加片段对象

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

    所以你可以随心所欲地对齐

    【讨论】:

      【解决方案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();
      

      【讨论】:

        【解决方案3】:

        这可能会有所帮助,

        <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" />
        

        【讨论】:

        • mmm 我在活动的 xml 中没有 标签。我有两个 xml 和两个 Fragment 类(每个片段一个)。我使用 FragmentTransaction 添加这两个片段。在两个片段类的 onCreateView 中,我膨胀对应的 xml 以创建并返回片段的视图
        • 你必须使用android SDK 3.0或更高版本。
        • 我的意思是我的活动视图是通过使用不包含任何片段的 xml 来膨胀的。我稍后添加片段。我正在使用兼容性支持库
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-21
        • 1970-01-01
        • 2013-08-10
        • 1970-01-01
        • 2014-01-11
        相关资源
        最近更新 更多