代码之家  ›  专栏  ›  技术社区  ›  Elisa

Android导航抽屉中的自定义颜色

  •  0
  • Elisa  · 技术社区  · 10 年前

    我正在开发一个Android应用程序,我必须定制一个导航抽屉,这是我使用的代码:

    <?xml version="1.0" encoding="utf-8"?>
    <menu
     xmlns:android="http://schemas.android.com/apk/res/android">    
      <item
       android:title="Discover">      <--- LABEL
       <menu>
        <item
         android:id="@+id/nav_qrcode"         <--- Item of menu
         android:icon="@drawable/read_qr"
         android:title="Read QRCode" />
    
        <item
         android:id="@+id/nav_favorite"          <--- Item of menu
         android:icon="@drawable/favorite"
         android:title="Favorite" />
       </menu>
     </item>
    </menu>
    

    这是抽屉布局:

    <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"
        android:background="@color/gold"
        app:itemTextColor="@color/white"
        app:menu="@menu/main_drawer" />
    </android.support.v4.widget.DrawerLayout>
    

    这是java代码:

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
    
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setItemIconTintList(null);
    

    我必须定制导航抽屉,标签的背景必须是“金色”,项目必须是“白色”。

    我怎么能做到这一点?

    1 回复  |  直到 10 年前
        1
  •  0
  •   Community Mohan Dere    9 年前

    第一种方法,您可以遵循以下主题: How to change the Text color of Menu item in Android?

    其次,您可以使用一个列表视图+自定义适配器,然后将此列表视图添加到DrawerLayout,而不是像这样在XML中定义。