简体   繁体   中英

Make the transparent actionbar in fragment

I've been searching on how to make transparent actionbar in fragment and no answer so far. So, the scenario is, I create an app using Navigation Drawer, each menu refer to same actionbar in MainActivity .

But I can't customize actionbar in transparent mode, like this Transparent Actionbar: custom tabcolor . In this:

getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#330000ff")));
actionBar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#550000ff")));

that code had to before setContentView , and as you know on the fragment we use onCreateView for the layout.

How can I achieve the transparent actionbar in fragment?

如果您只想制作透明的Actionbar,则可以在Fragment的onCreateView getActivity().getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33000000")));

Use a Toolbar and use the following code to let it behave like Action bar

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);

To include toolbar in your layout use the following.

<android.support.v7.widget.Toolbar
android:id=”@+id/my_awesome_toolbar”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
android:minHeight=”?attr/actionBarSize”
android:background=”?attr/colorPrimary” />

It is a lot easy to change transparency of Toolbar as compared to dealing with ActionBar

Once set you can deal with toolbar as any other view ie changing transparency/color dynamically is a lot easy for Toolbar

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