Android set transparent background for a fragment

旧城冷巷雨未停 提交于 2020-06-11 16:53:06

问题


In my app I have single activity and and all other fragments

I am setting background for activity from style.xml as below

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

Now for only a perticular Fragment I want to set background transparent and I am not able to do that tried below code in Fragment did 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 it?


回答1:


create a callback and implement it in Acitvity

interface OnFragmentDisplay{
  onFragmentDisplay();
}

when this fragment displays update Activity background to transparent ..or set it theme there in activity

See this link and this may help

have you tried this

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



回答2:


So, assuming you want the activity and fragment to be transparent for a particular fragment (It's not quite clear if this is what you want from your question, but I'm going to assume it is)..you need to set the "host" activity background to transparent when the "transparent" fragment is attached to it. When any other fragment attaches to it, you will need to reset the activity background or it will stay transparent..

There are a few ways of doing this, but I'm going to pick on something "fairly" straightforward:

in your transparent fragment:

      @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));
     }

Not entirely sure if this is the effect you want.

Good luck. Kind regards, CJ




回答3:


use null for color in style app

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



回答4:


You have to set your Activity also transparent.

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

Since your activity has @color/very_light_gray, even if you set your Fragment as @android:color/transparent it will not work.

Each view must have a transparent background to avoid overlapping.




回答5:


Your Activity container layout background should be transparent. Then set background color for Fragment's layouts. And for specific fragment's layout, set transparent color to get your required scenarios.




回答6:


You can use something like the code below in your activity while instantiating the fragment. In place of Color.White, you can use your own color.

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



回答7:


Simple way to do is set background color for root layout of that particular fragment as follows

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




回答8:


Add this single line in your code

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



回答9:


Set your activity layout's style as

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

and set it in activity's parent layout as

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

and in other fragments which you want to set white or other colored background, set different style in those fragment's main layout.

It will work




回答10:


This is not perfect solution but it works

Instead of using Fragment use DialogFragment

 //exploreCategory is argument
class MyFragment(val exploreCategory: ExploreCategory) : DialogFragment() {

    override fun onStart() {
      super.onStart()
      setWindowParams()
    }

   private fun setWindowParams(){
      dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
      dialog?.window?.setLayout(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT
      )
    }

}

Remain code is same like you write in fragment

For displaying use below code in fragment, if displaying in activity then use fragmentManager

MyFragment(exploreCategory).show(childFragmentManager,null)
//exploreCategory is argument, you can choose to send no arguments



回答11:


You've just set background for layout fragment below code:

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



回答12:


two steps:

first step - create a view (in an XML file) with a transparent background.

in android studio, locate "res" folder, right click it, new -> android resource file. Than in the appearing dialog put:

File name: {put here anything. for example - "my_fragment" (without the quotes)} notice that only lowercase letters, digits and underscores are ok

Resource type: Layout

Root element: LinearLayout (you can put here anything else after you will learn to know XML layouts better)

Source set: main

Directory name: layout.

In the window that will appear switch to "text" mode instead of "design" (look for the tabs in the lower-left side of the screen).

than copy-paste this instead of the file's content (it's very similar, we just add here the "transparent" background):

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

second step - inflate this layout in your fragment.

create your fragment class that extends Fragment and override the method onCreateView(), inside that method inflate your view and return it:

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

of course, don't forget to create an instance of your fragment in the activity!



来源:https://stackoverflow.com/questions/48974121/android-set-transparent-background-for-a-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!