Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

java - How to make Fragment background transparent?

I'm using Android Navigation Components with nav_graph and I want to make background of all fragments transparent. I tried to use the code from following resource: click For activity it's working fine, but in fragment background is always white.

public class SettingsFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

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

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_settings, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.setBackground(new ColorDrawable(Color.TRANSPARENT));
    }
}
question from:https://stackoverflow.com/questions/65952126/how-to-make-fragment-background-transparent

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The easiest way to solve this is to set the transparency of the Fragment in the XML.

You can define a transparent color in colors.xml like this: <color name="transparent">#00000000</color>

and then reference it in your fragment.xml like this: android:backgroundTint="@color/transparent"

Additionally its advisable, to set View-Altering commands into the onCreate().

Also you can define an android:alpha="0" attribute in the .xml to test if that will set the background color to transparent. But I guess this will only change the opacity of the whole fragment including child elements.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...