2016-10-27 3 views
-1

새로운 디자인 라이브러리 BottomNavigationView의 배경색을 색상 자원 대신 사용자 정의 색상 값으로 설정하는 방법이 있습니까? 어떤 "속임수"일까요?Design BottomNavigationView - 코드에서 배경색 설정

내 현재 솔루션 :

  • 는 내가이
  • 은 내가이보기의 배경

그러나 특히, 추한 외모를 업데이트 bottomNavigationView

  • 뒤의 두 번째보기를 투명 추가 BottomNavigationView하기 백그라운드보기에 대한 사용자 지정 동작을 사용하여 부모에있는 BottomNavigationView과 병행하여 애니메이션을 적용해야합니다....

  • 답변

    3

    직접 해결.

    해결 방법 1

    나는 단지 설정 투명한 배경을 가진 모든 항목 (이 하나의 리소스 파일이 필요합니다) 다음 I 테마 BottomNavigationView 실제 배경 자체.

    bottomBar.setBackground(new ColorDrawable(color)); 
    bottomBar.setItemBackgroundResource(R.drawable.transparent); 
    

    자원 그리기 - transparent.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
        <solid android:color="@android:color/transparent" /> 
    </shape> 
    

    용액 2 - 반사 비아 (지원 라이브러리 25.0.0)

    public void themeBottomBarBackgroundWithReflection(BottomNavigationView bottomBar, int color) 
    { 
        try 
        { 
         Field mMenuViewField = BottomNavigationView.class.getDeclaredField("mMenuView"); 
         mMenuViewField.setAccessible(true); 
         BottomNavigationMenuView mMenuView = (BottomNavigationMenuView)mMenuViewField.get(bottomBar); 
         Field mButtonsField = BottomNavigationMenuView.class.getDeclaredField("mButtons"); 
         mButtonsField.setAccessible(true); 
         BottomNavigationItemView[] mButtons = (BottomNavigationItemView[])mButtonsField.get(mMenuView); 
    
         for (BottomNavigationItemView item : mButtons) { 
          ViewCompat.setBackground(item, new ColorDrawable(color)); 
         } 
        } 
        catch (NoSuchFieldException e) 
        { 
         e.printStackTrace(); 
        } 
        catch (IllegalAccessException e) 
        { 
         e.printStackTrace(); 
        } 
    }