底部导航栏没有改变颜色

时间:2017-08-31 02:03:01

标签: android bottomnavigationview

我有一个带有四个片段的活动(MainActivity)(HomeFragment,callsFragment,chatsFragment,contactsFragment)。 Main Activity只有一个框架布局来放置HomeFragment。虽然内部homefragment包含viewpager和底部导航视图,但在更改页面底部导航文本和&图标颜色不会改变。为什么呢?

代码如下:

  

fragment_home.xml

<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.jona.bottomnavviewpager.HomeFragment"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_above="@+id/bottom_navigation"
        android:layout_alignParentTop="true"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        app:itemBackground="@color/colorPrimary"
        app:itemIconTint="@drawable/bottom_navigation_color_selector"
        app:itemTextColor="@drawable/bottom_navigation_color_selector"
        app:menu="@menu/menu_bottom_navigation"
        android:layout_alignParentBottom="true"
        />

</RelativeLayout>

  

bottom_navigation_color_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@color/tab_checked"  />
    <item android:state_checked="false" android:color="@color/tab_unchecked"  />
</selector>
  

menu_bottom_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_call"
        android:icon="@drawable/ic_call"
        android:title="@string/tab1"
        />
    <item
        android:id="@+id/action_chat"
        android:icon="@drawable/ic_chat"
        android:title="@string/tab2"
        />
    <item
        android:id="@+id/action_contact"
        android:icon="@drawable/ic_contacts"
        android:title="@string/tab3"
        />
</menu>
  

HomeFragment.java

public class HomeFragment extends Fragment {
private final static String TAG = "FragmentRumah";
private MenuItem prevMenuItem;

public HomeFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    //int argVal;
    final FragmentHomeBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);

    View view = binding.getRoot();

    setupViewPager(binding.viewpager);

    binding.bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.action_call:
                    binding.viewpager.setCurrentItem(0);
                    Log.d(TAG, "onNavigationItemSelected: action call");
                    break;
                case R.id.action_chat:
                    binding.viewpager.setCurrentItem(1);
                    Log.d(TAG, "onNavigationItemSelected: action chat");
                    break;
                case R.id.action_contact:
                    binding.viewpager.setCurrentItem(2);
                    Log.d(TAG, "onNavigationItemSelected: action contact");
                    break;
            }
            return false;
        }
    });

    binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            Log.d(TAG, "onPageScrolled");
        }

        @Override
        public void onPageSelected(int position) {
            Log.d(TAG, "onPageSelected");
            if (prevMenuItem != null){
                prevMenuItem.setChecked(false);
            } else {
                binding.bottomNavigation.getMenu().getItem(0).setChecked(false);
            }
            Log.d("page", "onPageSelected: "+position);
            binding.bottomNavigation.getMenu().getItem(0).setChecked(true);
            prevMenuItem = binding.bottomNavigation.getMenu().getItem(0);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            Log.d(TAG, "onPageScrollStateChanged");
        }
    });
    return view;
}

private void setupViewPager(ViewPager viewpager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
    adapter.addFragment(new callsFragment());
    adapter.addFragment(new chatFragment());
    adapter.addFragment(new contactsFragment());
    viewpager.setAdapter(adapter);
}

}

1 个答案:

答案 0 :(得分:0)

我通过在styles.xml中添加以下内容来改变这一点

<item name="android:navigationBarColor">@color/customColor</item>