Fragment is transparent and shows Activity below

前端 未结 5 852
旧巷少年郎
旧巷少年郎 2020-12-14 14:27

My Android application launches into BeginActivity which is a subclass of SherlockFragmentActivity and shows it\'s first view using:

@Override
    public voi         


        
相关标签:
5条回答
  • 2020-12-14 15:03

    Well, giving background to fragment is not always enough. For example if your background activity or fragment contains buttons, then we should set elevation for our FragmeLayout so that our fragment comes on top of other elements. Something like this:

        <FrameLayout
        android:elevation="5dp"
        ... />
    

    and then inside our fragment layout, we should set a background and make clickable attribute true to prevent working of buttons in background activity or fragment. Something like this :

    <androidx.constraintlayout.widget.ConstraintLayout
    ...
    android:background="@android:color/darker_gray"
    android:clickable="true">
    
    0 讨论(0)
  • 2020-12-14 15:10

    IMHO I do not agree with this answer.

    You may want to replace your fragment or you may want to add it.

    For example let's say that you are in a fragment that is a list retrieved by a network request, if you replace the fragment with let's say the detailFragment and you add it to backStack.

    When you go back your fragment will redo the network query, of course you can cache it but why? It is a back to a previous state, so with add the last fragment will be in exactly the same status with no code whatsoever.

    Fragments are transparent backgrounded by default because they can be used to paint only a small portion of the screen but if your fragment is match_parent then just set its background to a color and keep using add on the fragmentTransaction.

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:background="@android:color/white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    

    That will be your root element on the fragment layout XML, could be linear, etc etc and the transaction code is:

    YourFragment detail = YourFragment.newInstance(Id);
    ft.add(R.id.contentHolder, detail);
    ft.addToBackStack(TAG);
    ft.commit();
    

    Hope this helps someone that wants to know how to see a solid background without changing the add to replace which is usually not the best case.

    Regards

    0 讨论(0)
  • 2020-12-14 15:21

    Nobody mentioned that if you added a background to your Fragment but you are still getting a transparent background, you need to check that your FrameLayout is the last view in your XML.

    Because z-index in Android is calculated by XML position and lowest view in the hierarchy have the highest z-index.

    For example I fixed this problem recently simply moving the FrameLayout from here:

    <androidx.coordinatorlayout.widget.CoordinatorLayout
      ...>
    
        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
        <RelativeLayout
          ...>
        </RelativeLayout>
    
        <RelativeLayout
          ...>
        </RelativeLayout>
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    to here:

    <androidx.coordinatorlayout.widget.CoordinatorLayout
      ...>
    
        <RelativeLayout
          ...>
        </RelativeLayout>
    
        <RelativeLayout
          ...>
        </RelativeLayout>
    
        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    
    0 讨论(0)
  • 2020-12-14 15:22

    Try using FragmentTransaction class to replace the fragments instead of just adding.

    Explanation:

    Each transaction is a set of changes that you want to perform at the same time. You can set up all the changes you want to perform for a given transaction using methods such as add(), remove(), and replace(). Then, to apply the transaction to the activity, you must call commit().

    Before you call commit(), however, you might want to call addToBackStack(), in order to add the transaction to a back stack of fragment transactions. This back stack is managed by the activity and allows the user to return to the previous fragment state, by pressing the Back button.

    For example, here's how you can replace one fragment with another, and preserve the previous state in the back stack:

    Example:

    // Create new fragment and transaction
    Fragment newFragment = new ExampleFragment();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    
    // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack(null);
    
    // Commit the transaction
    transaction.commit();
    

    Reference: Please have a look at Managing Fragments

    I hope it will be helpful !!

    0 讨论(0)
  • 2020-12-14 15:25

    i used many techniques but no gain Finally i fixed it by adding background of the container or the fragment layout like this

    android:background="@android:color/white"    
    

    you just need to add in your Layout or the container view of you Fragment Hope it will help

    0 讨论(0)
提交回复
热议问题