简体   繁体   中英

How to change toolbar menu item background color?

I need a way to change the background color of menu item just how twitter app does before showing a picture in full-screen mode.

在此处输入图片说明

You can get the item and have it as a MenuItem, then you can use setActionView to use a layout xml file which you have to inflate before, something like this look:

MenuItem alerts;

Then on your onCreateOptionsMenu:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);

        alerts = menu.findItem(R.id.alerts);
        ...

And then wherever you need:

View view = getLayoutInflater().inflate(R.layout.notification_badge, null);
    view.setOnClickListener(new View.OnClickListener() {
        @Override
            public void onClick(View v) {
                startActivity(new Intent(v.getContext(), NotificationsActivity.class));
            }
        });

And finally:

alerts.setActionView(view);

You meet to create custom Tab icons best example is as below

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);

        viewPager.setAdapter(new TabPagerAdapter(getSupportFragmentManager()));

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);

        TabLayout.Tab tabCall = tabLayout.getTabAt(0);
        tabCall.setIcon(R.drawable.selector_call);

        TabLayout.Tab tabHeart = tabLayout.getTabAt(1);
        tabHeart.setIcon(R.drawable.selector_heart);

        TabLayout.Tab tabContacts = tabLayout.getTabAt(2);
        tabContacts.setIcon(R.drawable.selector_contacts);
    }

    class TabPagerAdapter extends FragmentPagerAdapter {

        public TabPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public Fragment getItem(int position) {

            switch (position) {
                case 0:
                    return new CallFragment();
                case 1:
                    return new HeartFragment();
                case 2:
                    return new ContactsFragment();
            }

            return null;
        }
    }
}

Use a selector :

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:state_selected="true"
        android:drawable="@drawable/contacts_selected" />

    <item
        android:state_selected="false"
        android:drawable="@drawable/contacts_unselected" />
</selector>

For more details reference :

https://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

You need to create a package color inside the res folder, inside the color package create a XML file like this :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@color/colorWhite" />
    <item android:color="@color/colorLightGrey"  />
</selector>

This xml will put your item in white, when they are selected ang grey otherwise.

For using this file you simply need to add this to your bottom navigation view :

app:itemIconTint="@color/nav_item_state_list"
app:itemTextColor="@color/nav_item_state_list"

nav_item_state_list is the xml file above.

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