2016-01-10 17 views
6

Tôi đã tạo Navigation Drawer và tôi thấy playtore có biểu tượng menu màu tôi muốn biết làm thế nào tôi có thể làm điều này. Tôi cố gắng để áp dụng màu sắc bởi colorFilter vào biểu tượng trình đơn nhưng lực lượng ứng dụng đóngLàm cách nào để đưa màu cho các mục menu cho ngăn Điều hướng?

My app

what i wanted

Đây là mã của tôi

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
<group 
    android:id="@+id/grp1" 
    android:checkableBehavior="single"> 
    <item 
     android:id="@+id/navigation_songs" 
     android:checked="true" 
     android:icon="@drawable/ic_audiotrack_white_24dp" 
     android:title="@string/songs" /> 
    <item 
     android:id="@+id/navigation_albums" 
     android:icon="@drawable/ic_album_white_24dp" 
     android:title="@string/albums" /> 
    <item 
     android:id="@+id/navigation_artist" 
     android:icon="@drawable/ic_person_white_24dp" 
     android:title="@string/artists" /> 
    <item 
     android:id="@+id/navigation_playlist" 
     android:icon="@drawable/ic_playlist_play_white_24dp" 
     android:title="@string/playlist" /> 
</group> 
<group 
    android:id="@+id/grp2" 
    android:checkableBehavior="none"> 
    <item 
     android:id="@+id/navigation_about" 
     android:icon="@drawable/ic_info_white_24dp" 
     android:title="@string/about" /> 
    <item 
     android:id="@+id/navigation_settings" 
     android:icon="@drawable/ic_settings_white_24dp" 
     android:title="@string/settings" /> 
</group> 
</menu> 

Trả lời

10
  1. Để chải tất cả các biểu tượng vào particu lar màu sắc, bạn cần phải thêm app:itemIconTint vào NavigationView của bạn:

    <android.support.design.widget.NavigationView 
        ........ 
        app:itemIconTint="<your color>"/> 
    

    enter image description here

  2. Để chải biểu tượng của bạn chỉ trong 2 màu:

    Tạo một Selector:

    <?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
        <item android:color="#0000FF" android:state_checked= "true" /> 
        <item android:color="#FF0000" /> 
    </selector> 
    

    Và áp dụng nó như một app:itemIconTint trong y chúng tôi NavigationView:

    <android.support.design.widget.NavigationView 
        ........ 
        app:itemIconTint="@drawable/tint_color_selector"/> 
    

    Bước cuối cùng - để thêm android:checked="true" đến MenuItems trong xml menu ngăn kéo của bạn, bạn muốn chải khác nhau:

    <item 
        android:id="@+id/nav_slideshow" 
        android:checked="true" 
        android:icon="@drawable/ic_menu_slideshow" 
        android:title="Slideshow" /> 
    

    enter image description here

  3. Để chải tất cả các biểu tượng trong khác nhau màu sắc, như Google có trong Google Play:

    Disable pha màu của các biểu tượng của bạn:

    navigationView.setItemIconTintList(null); 
    

    Thêm biểu tượng bạn muốn các res/drawable và xác định chúng là android:icon trong xml của thực đơn của bạn. (Tôi có thể giới thiệu một dịch vụ tốt đẹp cho các biểu tượng android icons8.com/web-app/new-icons/android)

    enter image description here

    Thay vì tải lên các biểu tượng mới đầy màu sắc, bạn có thể vẽ những cái hiện có, nhưng nó rất hacky:

    Drawable oldIcon = navigationView 
          .getMenu() 
          .findItem(R.id.nav_gallery) 
          .getIcon(); 
    
        if (!(oldIcon instanceof BitmapDrawable)) { 
         return; 
        } 
    
        Bitmap immutable = ((BitmapDrawable)oldIcon).getBitmap(); 
        Bitmap mutable = immutable.copy(Bitmap.Config.ARGB_8888, true); 
        Canvas c = new Canvas(mutable); 
        Paint p = new Paint(); 
        p.setColor(Color.RED); 
        p.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY)); 
        c.drawBitmap(mutable, 0.f, 0.f, p); 
        BitmapDrawable newIcon = new BitmapDrawable(getResources(), mutable); 
    
        navigationView 
          .getMenu() 
          .findItem(R.id.nav_gallery) 
          .setIcon(newIcon); 
    

    Coi chừng! Trong res/drawables-v21 trong dự án mẫu, Google sử dụng VectorDrawable s thay vì cũ BitmapDrawables, vì vậy mã này sẽ không hoạt động ở đó.

Tôi hy vọng điều đó sẽ hữu ích.

+0

Xin chào, tôi có thể thay đổi màu văn bản của tiêu đề phụ như (Giao tiếp)?Tôi sẽ tìm kiếm tất cả các xung quanh nhưng tôi không thể tìm thấy..Xin vui lòng cung cấp cho tôi một số thay đổi tùy chỉnh. –

+0

Rất tuyệt! giải thích tốt nhất tôi đã nhìn thấy. Nhà phát triển có thể đặt màu biểu tượng theo chủ đề không? –

+0

Giải pháp hoàn hảo –

Các vấn đề liên quan