2016-10-27 17 views
-1

新しいデザインライブラリBottomNavigationViewの背景色を、色のリソースではなくカスタムの色の値に設定する方法はありますか?どんな "トリック"かもしれない?Design BottomNavigationView - コードの背景色を設定

私の現在のソリューション:

  • 私はBottomNavigationView透明
  • が、私は特に、私はこのビューの背景

を更新しかし、これは醜いbottomNavigationView

  • の背後にある2番目のビューを追加しますバックグラウンドビューのカスタムビヘイビアを親のBottomNavigationViewと並行してアニメートする必要があるため、CoordinatorLayout ...

  • 答えて

    3

    自分で解決しました。

    ソリューション1

    Iちょうどセットアップ透明な背景を持つすべての項目(これは一つだけのリソースファイルが必要)、その後、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(); 
        } 
    } 
    
    関連する問題