简体   繁体   中英

Set fragments' positions in android activities

I have a fragment that should be shown in an activity under another fragment

In the xml of the fragment I tried to use:

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

(please note android:layout_below="@id/first_fragment" in the layout tag)

I have also tried this:

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

(please note android:layout_below="@id/first_fragment" in the TextView tag)

in both cases the application compile and run but the second fragment is shown on the top of the screen instead of after the first one.

Please consider that I'm adding the fragments programmatically by using FragmentTransaction and I add the second fragment after having added the first one, but in the same transaction

Can you please tell me what's wrong?

Thank-you

You can use a FrameLayout (viewGroup) in your activity xml to hold your fragment, then add your fragment object using add or replace

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

So you can align as you wish

I fixed it doing this. In the layout I used:

<?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>

then in the activity:

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();

This may be help,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM