底部菜单控件BottomNavigationView的使用 |
您所在的位置:网站首页 › 安卓导航栏颜色 › 底部菜单控件BottomNavigationView的使用 |
BottomNavigationView实现底部导航栏的整体效果如下图所示:
BottomNavigationView控件的主要属性含义: app:iteamBackground 指的是底部导航栏的背景颜色,默认是主题的颜色app:itemIconTint 指的是底部导航栏中图片的颜色app:itemTextColor 指的是底部导航栏文字的颜色app:menu 指的是菜单布局(文字和图片都写在这个里面) app:menu="@menu/menu_navigation_fix"中的界面 控件点击时候颜色选择@color/sl_color_green_grey 完整的MainActivity代码 public class MainActivity extends AppCompatActivity { private FrameLayout mainFrame; private BottomNavigationView bottomNavigation; private MyFragment messageFragment, findFragment, circleFragment; private Fragment[] fragments; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { messageFragment = new MyFragment(); messageFragment.setMessage(getResources().getString(R.string.message)); findFragment = new MyFragment(); findFragment.setMessage(getResources().getString(R.string.find)); circleFragment = new MyFragment(); circleFragment.setMessage(getResources().getString(R.string.circle)); fragments = new Fragment[]{messageFragment, findFragment, circleFragment}; mainFrame = findViewById(R.id.mainContainer); //设置fragment到布局 getSupportFragmentManager() .beginTransaction() .replace(R.id.mainContainer, messageFragment) .commit(); bottomNavigation = findViewById(R.id.navigation); //这里是bottomnavigationview的点击事件 bottomNavigation.setOnNavigationItemSelectedListener(listener); } private BottomNavigationView.OnNavigationItemSelectedListener listener = new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { int id = item.getItemId(); if (id == R.id.action_message) switchFragment(fragments[0]); else if (id == R.id.action_find) switchFragment(fragments[1]); else if (id == R.id.action_circle) switchFragment(fragments[2]); return true; } }; // 切换fragment private void switchFragment(Fragment fragment) { FragmentManager fm = getSupportFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); transaction.replace(R.id.mainContainer, fragment); transaction.commitAllowingStateLoss(); } }运行的效果图如下: item多的时候,指的是item非选中状态显示文字,有以下几个值: labeled 图标文字都显示selectedautounlabeled 文字不显示它们依次运行如下图所示 设置了这个之后导航栏高度就变高了,同时图片和文字之间的距离就变大了,具体是多少我也不清楚 其他的设置都是瞎搞; 先说一下这里的 app:itemBackground 和app:itemRippleColor都是设置给到导航栏的背景 如果要设置水波纹有有效果,那么必须不设置背景: app:itemBackground="@null" app:itemRippleColor="@color/design_default_color_primary"只有这样才会有效果;否则无效; 这是在其源码里面发现的: int itemBackground = a.getResourceId(R.styleable.BottomNavigationView_itemBackground, 0); if (itemBackground != 0) { menuView.setItemBackgroundRes(itemBackground); } else { ColorStateList itemRippleColor = MaterialResources.getColorStateList( context, a, R.styleable.BottomNavigationView_itemRippleColor); setItemRippleColor(itemRippleColor); }再看看 setItemRippleColor(itemRippleColor);方法,最后还是把水波纹效果设置给了 menuView.setItemBackgroundRes(); public void setItemRippleColor(@Nullable ColorStateList itemRippleColor) { if (this.itemRippleColor == itemRippleColor) { // Clear the item background when setItemRippleColor(null) is called for consistency. if (itemRippleColor == null && menuView.getItemBackground() != null) { menuView.setItemBackground(null); } return; } this.itemRippleColor = itemRippleColor; if (itemRippleColor == null) { menuView.setItemBackground(null); } else { ColorStateList rippleDrawableColor = RippleUtils.convertToRippleDrawableColor(itemRippleColor); if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { menuView.setItemBackground(new RippleDrawable(rippleDrawableColor, null, null)); } else { GradientDrawable rippleDrawable = new GradientDrawable(); // TODO: Find a workaround for this. Currently on certain devices/versions, LayerDrawable // will draw a black background underneath any layer with a non-opaque color, // (e.g. ripple) unless we set the shape to be something that's not a perfect rectangle. rippleDrawable.setCornerRadius(0.00001F); Drawable rippleDrawableCompat = DrawableCompat.wrap(rippleDrawable); DrawableCompat.setTintList(rippleDrawableCompat, rippleDrawableColor); menuView.setItemBackground(rippleDrawableCompat); } } } 切换自己设定选中图片和未选中图片想要切换自己UI设计师给的两种不同的Ui图片; 除去自带着色效果navigation.setItemIconTintList(null) 除去自带着色效果 (在XML中app:itemIconTint=@null无效,需要代码设置) 创建一个drawable选择器有几个item就创建几个,比如sl_drawable_circle.xml 设置相应item中android:icon属性找到相应item中 android:icon属性
|
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |