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

时间:2017-02-01 06:33:18

标签: java android

我在Android工作室的新功能,我使用导航抽屉为我的应用程序。但我不知道默认情况下如何更改导航抽屉上每个图标。我想用自己的图标请帮助!

我要更改图标:

enter image description here

以及此项目

enter image description here



<?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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

您只需使用工具栏并为其设置导航图标即可。

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 :(得分:0)

1)在R文件夹中创建一个菜单文件夹

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>

在NavigaitonView标签中添加此菜单文件

3)如果要为导航视图创建自己的工具栏。 在布局文件夹中创建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)在布局文件中添加此工具栏

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

5)转到您要创建导航视图的活动并执行以下操作

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);
                }
            }
        });

 }
 }

***别忘了为你的布局文件添加NoActionBar

答案 2 :(得分:0)

当我使用android studio提供的默认模板创建导航视图时,我遇到了同样的问题。我想更改导航视图标题的背景并放置我自己的图标。

但是我使用简单的技术逐步向下导航抽屉的标题。请从NavigationView

下的代码中检查此行
app:headerLayout="@layout/nav_header_main"

这是布局文件夹下的导航标题布局。打开此文件并在此处放置代码。我的标题看起来像这样,我已经为此分配了漂亮的黑色背景(带渐变)。

要分配我自己的应用程序图标,我在图像视图下提供了自己的drawable(或mipmap)。请注意,此ImageView已由默认模板放置。 P.S 这是我导航标题的修剪代码,不想用不必要的代码压倒你。

<?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>