简体   繁体   English

如何更改导航抽屉及其项目android studio的图标

[英]How can I change the icon of the navigation drawer and its item android studio

im new on android studio and I use the navigation drawer for my app. 即时消息是android studio上的新功能,我将导航抽屉用于我的应用。 but I don't know how to change the icon of each on navigation drawer by default. 但我不知道默认情况下如何更改导航抽屉上每个图标的图标。 I want to use my own icon PLEASE HELP! 我想使用我自己的图标,请帮助!

This I want to change the icon: 我要更改此图标:

在此处输入图片说明

and also of this item 还有这个项目

在此处输入图片说明

 <?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" app:itemIconTint="#000000"/> </android.support.v4.widget.DrawerLayout> 

you just need to use toolbar and set navigation icon for it. 您只需要使用工具栏并为其设置导航图标。

toolbar = (Toolbar) findViewById(R.id.toolbar);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view_main_activity);
    navigationView.setNavigationItemSelectedListener(this);
    toolbar.setNavigationIcon(R.mipmap.ic_menu);

1)create a menu folder in R folder 1)在R文件夹中创建一个菜单文件夹

2)now in menu create navigation_menu.xml file : paste this code - add icons from drawable folder 2)现在在菜单中创建navigation_menu.xml文件:粘贴此代码-从可绘制文件夹中添加图标

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <group android:checkableBehavior="all">
        <item
            android:id="@+id/home"
            android:title="Home"
            android:icon="@drawable/user"
            />

        <item
            android:id="@+id/earn"
            android:title="Wallet"
            android:icon="@drawable/earn"
            />
        <item
            android:id="@+id/notifications"
            android:title="Notifications"
            android:icon="@drawable/notification"
            />
    </group>
    <group android:checkableBehavior="single"
        android:menuCategory="secondary"
        android:id="@+id/secondary">
        <item
            android:title="Help"
            android:visible="false"
            android:id="@+id/help"
            android:icon="@drawable/ic_action_help"
            />
        <item
            android:title="About Us"
            android:id="@+id/aboutus"
            android:icon="@drawable/ic_action_name"
            >
        </item>
    </group>
</menu>

add this menu file in NavigaitonView tag 将此菜单文件添加到NavigaitonView标记中

3)if you want to create your own toolbar for Navigation view. 3)如果要为导航视图创建自己的工具栏。 create toolbar.xml in your layout folder and paste this code ** add image in ImageView 在您的布局文件夹中创建toolbar.xml并粘贴此代码**在ImageView中添加图像

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:elevation="10dp"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_hamburger"
        android:id="@+id/hamburger"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Hello"
        android:textColor="@color/light_grey"
        android:gravity="center"
        android:padding="@dimen/small_padding"
        android:textSize="14dp"
        android:id="@+id/title_text"/>

</android.support.v7.widget.Toolbar>

4)add this toolbar in layout file 4)将此工具栏添加到布局文件中

  <include
            layout="@layout/toolbar"
            android:id="@+id/toolbar"/>

5) goto Activity where you want to create Navigation View and do the following 5)转到要在其中创建导航视图的Activity并执行以下操作

public class MainActivity extends BaseActivity {


    TextView titleToolbarTV;
    ImageView  hamburgerIV;
    DrawerLayout drawerLayout;
    NavigationView navigationView;
    Toolbar toolbar; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  

        titleToolbarTV = (TextView)findViewById(R.id.title_text); 

        toolbar = (Toolbar)findViewById(R.id.toolbar);
        hamburgerIV = (ImageView)findViewById(R.id.hamburger);
        drawerLayout = (DrawerLayout) findViewById(R.id.base_layout);
        navigationView = (NavigationView)findViewById(R.id.navigation_drawer);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("");
        loginPreferences = new LoginPreferences(MainActivity.this);
        user = loginPreferences.getUser();
        hamburgerIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(drawerLayout.isDrawerOpen(GravityCompat.START)){
                    drawerLayout.closeDrawer(GravityCompat.START);
                }
                else{
                    drawerLayout.openDrawer(GravityCompat.START);
                }
            }
        });

 }
 }

***dont forget to add NoActionBar for your layout file ***不要忘记为您的布局文件添加NoActionBar

When I created a navigation view using default template provided by android studio, I faced same problem. 当我使用android studio提供的默认模板创建导航视图时,遇到了同样的问题。 I wanted to change the background of the navigation view header and place my own icon. 我想更改导航视图标题的背景并放置自己的图标。

But I used simple technique to go step wise down to the header of navigation drawer. 但是我使用了简单的技术逐步导航到导航抽屉的标题。 Please check this line from your code under NavigationView 请在NavigationView下的代码中检查此行

app:headerLayout="@layout/nav_header_main"

This is the navigation header layout under folder layout . 这是文件夹布局下的导航标题布局 Open this file and place code here. 打开此文件,然后在此处放置代码。 My header looks like this, I have assigned the nice black background (with gradient) to this. 我的标题看起来像这样,我为此分配了漂亮的黑色背景(带有渐变)。

To assign my own application icon, I have provided my own drawable (or mipmap) under the image view. 为了分配自己的应用程序图标,我在图像视图下提供了自己的drawable(或mipmap)。 Notice this ImageView has been placed by the default template. 请注意,此ImageView已由默认模板放置。 PS this is trimmed code of my navigation header, do not want to overwhelm you with unnecessary code. PS这是我的导航标题的修剪代码,不想以不必要的代码让您不知所措。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar">

   <!--My app icon-->
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@mipmap/ic_launcher" />

    <!--My company/product name-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="@string/IDS_0235"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textColor="@color/colorTextLight"/>

    <!--My website link-->
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

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

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