答案 0 :(得分:0)
您可以获取该项目并将其作为MenuItem,然后您可以使用setActionView来使用之前必须膨胀的布局xml文件,如下所示:
MenuItem alerts;
然后在你的onCreateOptionsMenu:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
alerts = menu.findItem(R.id.alerts);
...
然后随时随地:
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));
}
});
最后:
alerts.setActionView(view);
答案 1 :(得分:0)
您见面创建自定义标签图标最佳示例如下
公共类MainActivity扩展了AppCompatActivity {
team pts/g
St. Louis Rams 32.875
Washington Redskins 27.687
Minnesota Vikings 24.937
Indianapolis Colts 26.437
Oakland Raiders 24.375
Carolina Panthers 26.312
Jacksonville Jaguars 24.75
Chicago Bears 17.0
Green Bay Packers 22.312
San Francisco 49ers 18.437
Buffalo Bills 20.0
}
使用选择器:
@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;
}
}
有关详细信息,请参阅:
https://www.androidhive.info/2015/09/android-material-design-working-with-tabs/
答案 2 :(得分:0)
你需要在res文件夹里面创建一个包颜色,在颜色包里面创建一个这样的XML文件:
<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>
此项xml会将您的项目设为白色,否则选择为灰色。
要使用此文件,您只需将其添加到底部导航视图:
app:itemIconTint="@color/nav_item_state_list"
app:itemTextColor="@color/nav_item_state_list"
nav_item_state_list是上面的xml文件。