Android sets transparent background for fragment

In my application, I have one action and all other fragments

I set the background for activity from style.xml as below

<item name="android:windowBackground">@color/very_light_gray</item>

Now, only for the percussion fragment, I want to set a transparent background, and I can not do this, because under the code below in Fragment does not work for me

  @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// create ContextThemeWrapper from the original Activity Context with the custom theme
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme);

// clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

// inflate the layout using the cloned inflater, not default inflater
return localInflater.inflate(R.layout.yourLayout, container, false);
}

Any idea how to do this?

+6
source share
13 answers

create a callback and implement it in Acitvity

interface OnFragmentDisplay{
  onFragmentDisplay();
}

when this fragment displays an update of the background for transparent .. or set it there in activity

.

fragment.getView().setBackgroundColor(Color.WHITE);
+1

:

<LinearLayout
...
android:background="@android:color/transparent"/>
0

:

- ( XML) .

Android, "res", , new -> android resource file. :

File name: { . , " my_fragment" ( )} , ,

Resource type:

Root element: LinearLayout ( - , XML)

Source set: main

Directory name:.

"" "" ( ).

copy-paste, ( , "" ):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    >

</LinearLayout>

- .

, onCreateView(), :

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    // assuming you named your xml file "my_fragment":
    // for other names replace R.layout.my_fragment with  R.layout.put_your_name_here
    View view = inflater.inflate(R.layout.my_fragment, container, false);
    return view;
}

, !

0

null

 <item name="android:windowBackground">null</item>
0

.

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

@color/very_light_gray, @android:color/transparent, .

, .

0

. . , .

0

- . Color.White .

fragment.getView().setBackgroundColor(Color.WHITE);
0

-

Android: = "@: /"

0

, , , ( , , , , ).. "" , "" . , reset .

, - "" :

:

      @Override
      public void onStart() {
        super.onStart();
// I'm using null here because drawing nothing is faster than drawing transparent pixels.
        getActivity().getWindow().setBackgroundDrawable(null);
        getView().setBackground(null);
      }

      @Override
      public void onStop() {
        super.onStop();
        getActivity().getWindow().setBackgroundDrawable(new ColorDrawable(@color/very_light_gray));
     }

, , .

. , CJ

0

getActivity().getWindow().setBackgroundDrawableResource(R.drawable.my_drawable);
0

<style name="RootLayout" parent="AppTheme">
    <item name="android:background">@drawable/app_transparent_background</item>
</style>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    style="@style/RootLayout"
    ...>

, , .

0

, onCreateView() ,

getActivity().getWindow().getDecorView().setBackgroundColor(getResources().getColor(android.R.color.transparent));

, "android:windowBackground" style.xml, , , onDestroy().

0

, Background

XML , .

android:background="@android:color/white"
-1
source

All Articles