简体   繁体   English

Android导航抽屉自定义图标

[英]Android Navigation drawer custom icons

I've currently been looking into the navigation drawer of android and see the example code from android they use. 我目前一直在研究android的导航抽屉,并查看他们使用的android示例代码。

When starting a new project and selecting the navigation drawer activity. 启动新项目并选择导航抽屉活动时。 It gives basic structure which is great. 它给出了很棒的基本结构。

From this I see the icons used in the navigation drawer are android default. 由此可见,导航抽屉中使用的图标是android默认设置。 I wanted to see if possible to add my own png for icons. 我想看看是否可以为图标添加自己的png。 So far my code (in activity_main_drawer) is 到目前为止,我的代码(在activity_main_drawer中)是

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_connect"
        android:icon="@drawable/icon_menu_connect_24"
        android:title="Connect" />
    <item
        android:id="@+id/nav_settings"
        android:icon="@drawable/icon_menu_settings_24"
        android:title="Settings" />
    <item
        android:id="@+id/nav_help"
        android:icon="@drawable/icon_menu_help_24"
        android:title="Help" />

</group>

the _24 at the end of the name is just the 24pixel size indication. 名称末尾的_24只是24像素大小的指示。

Now after running this code all i get appear in my navigation drawer is square blocks and not the image. 现在,运行此代码后,所有显示在导航抽屉中的都是正方形块而不是图像。

Reason for using own png rather than android default, is looking at the defaults there was no images indicating what i wanted. 使用自己的png而不是android默认值的原因是,查看默认值时没有图像指示我想要什么。

Appollogies if question has been posted before and thanks in advance 如果问题已在之前发出并提前表示感谢,则表示歉意

I have found the issue. 我发现了问题。 I found that the image used in the menus need to have a transparent background. 我发现菜单中使用的图像需要具有透明背景。 The images I was using did not get saved in that format and thus showed as a grey block. 我使用的图像没有以该格式保存,因此显示为灰色块。 So although it gave a preview in the left hand margin in the XML file, it still was not in the right format 因此,尽管它在XML文件的左边缘提供了预览,但它的格式仍然不正确

Using image asset to generate the icons was more efficient than lots of .PNG files 使用图像资产生成图标比许多.PNG文件更有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM