2016-01-19 20 views
9

Tôi muốn nhập biểu tượng tùy chỉnh của mình vào menu trong ngăn điều hướng. Tuy nhiên, khi tôi chạy ứng dụng nó chỉ hiển thị một bóng của biểu tượng. Làm cách nào để hiển thị hình ảnh chính xác?Biểu tượng menu ngăn kéo điều hướng không được hiển thị chính xác

enter image description here

Dưới đây là file xml cho menu activity_main_drawer.xml:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <group android:checkableBehavior="single"> 
     <item 
      android:id="@+id/recent_picture" 
      android:icon="@mipmap/ic_recent" 
      android:title="Import" /> 
     <item 
      android:id="@+id/nav_gallery" 
      android:icon="@drawable/ic_menu_gallery" 
      android:title="Gallery" /> 
     <item 
      android:id="@+id/nav_slideshow" 
      android:icon="@drawable/ic_menu_slideshow" 
      android:title="Slideshow" /> 
     <item 
      android:id="@+id/nav_manage" 
      android:icon="@drawable/ic_menu_manage" 
      android:title="Tools" /> 
    </group> 
</menu> 

Dưới đây là cách bố trí xml với giao diện điều hướng:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 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" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 
</android.support.v4.widget.DrawerLayout> 

Có drawables.xml khác các tệp được tìm thấy trong thư mục giá trị.

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 
<item name="ic_recent_picture" type="drawable">@mipmap/ic_recent</item> 
<item name="ic_menu_gallery" type="drawable">@drawable/ic_menu_gallery</item> 
<item name="ic_menu_slideshow" type="drawable">@android:drawable/ic_menu_slideshow</item> 
<item name="ic_menu_manage" type="drawable">@android:drawable/ic_menu_manage</item> 
<item name="ic_menu_share" type="drawable">@android:drawable/ic_menu_share</item> 
<item name="ic_menu_send" type="drawable">@android:drawable/ic_menu_send</item> 

+0

Cố gắng đặt hình ảnh vào thư mục 'drawable' –

+0

@cwfei, tôi đã thử đưa hình ảnh vào thư mục drawable nhưng nó cũng trả về cùng một lỗi. – michelletbs

+0

Bạn đã cập nhật tham chiếu trong tệp xml của mình thành 'android: icon =" @ drawable/ic_recent "' khi bạn di chuyển hình ảnh vào thư mục drawable? –

Trả lời

0

Bạn đã cố gắng thay đổi để hình ảnh khác để xem nếu nó đang hiển thị?

<item name="ic_recent_picture" type="drawable">@mipmap/ic_recent</item> 

Đường dẫn @mipmap chứa hình ảnh chính xác?

+0

Tôi đã thay thế bằng hình ảnh đã được tạo trước từ mẫu, hoạt động của nó. android: icon = "@ drawable/ic_menu_gallery", nhưng nếu tôi đặt hình ảnh tùy chỉnh của riêng tôi thì nó sẽ hiển thị hình vuông màu xám. – michelletbs

+0

Đặt hình ảnh vào thư mục có thể vẽ được ... –

+0

Xin chào @Weverton Peron, nó hoạt động ngay bây giờ bằng cách nhập mã này để loại bỏ chế độ màu từ biểu tượng. navigationView.setItemIconTintList (null); – michelletbs

23

Nó hoạt động ngay bây giờ bằng cách xóa hiệu ứng màu khỏi biểu tượng theo setItemIconTintList(null). Đặt dòng mã bên dưới theo số navigationView.

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
navigationView.setItemIconTintList(null); 
navigationView.setNavigationItemSelectedListener(this); 
+1

Cảm ơn Michelletbs, navigationView.setItemIconTintList (null); giải quyết vấn đề của tôi. – Sally

+0

Giải pháp tuyệt vời, hoạt động rất tốt. Có vẻ là tương thích ngược, kẹo trước vẫn hoạt động. Cảm ơn! – TryTryAgain

+0

Tuyệt vời! Một giải pháp khác, nhưng chỉ để trao đổi 2 màu là ở đây: http://stackoverflow.com/questions/30967851/change-navigation-view-item-color-dynamicly-android. – CoolMind

0

nếu bạn vẫn chưa giải quyết được vấn đề bằng cách loại bỏ các màu, bạn có thể muốn di chuyển tập tin tài nguyên của bạn để có thể vẽ được (sử dụng Poject View) thay vì drawable-anydpi như tôi đã làm

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